class Solution(object):
    def removeNthFromEnd(self, head, n):
        """
        :type head: ListNode
        :type n: int
        :rtype: ListNode
        """

        dummy = ListNode(-1)
        dummy.next = head
        p = dummy
        c = head
        c2 = head
        num = 0
        while c2 is not None:
            num+=1
            c2=c2.next
        for i in range(num-n+1):
            if c.next is not None:
                p=p.next
                c=c.next
            else:
                p.next = None
                return dummy.next  
        p.val = c.val
        p.next=c.next
        return dummy.next
Copy the code