leetcode remove Nth Node from End python

# Definition for singly-linked list.
# class ListNode(object):
#     def __init__(self, x):
#         self.val = x
#         self.next = None

class Solution(object):
    def removeNthFromEnd(self, head, n):
        """
        :type head: ListNode
        :type n: int
        :rtype: ListNode
        """
        dummy=ListNode(0)
        dummy.next=head
        
        p1=p2=dummy
        
        for i in range(n):
            p1=p1.next
        while p1.next:
            p1=p1.next
            p2=p2.next
        p2.next=p2.next.next
            
        return dummy.next

@link http://www.cnblogs.com/zuoyuan/p/3701971.html

posted @ 2015-11-21 22:35  hao.ma  阅读(109)  评论(0编辑  收藏  举报