Python recursion solution. Easy to understand


  • 1
    R
    class Solution(object):
    def __init__(self):
        self.c=0
        self.h=None
    def ree(self,head,pre,n):
        if not head:
            return 1
        else:
            c=self.ree(head.next,head,n)
            if c==1:
                self.c+=c
                if self.c==n:
                    if pre:
                        pre.next=head.next
                        return self.h
                    else:
                        self.h=self.h.next
                        return self.h
                else:
                    return 1
            else:
                return self.h
    def removeNthFromEnd(self, head, n):
        """
        :type head: ListNode
        :type n: int
        :rtype: ListNode
        """
        self.h=head
        return self.ree(head,None,n)

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.