Python Single Pass Solution


  • 0
    B
    class Solution(object):
        def removeNthFromEnd(self, head, n):
            h, p = head, head
            x, length = 0, 0
            while h:
                length += 1
                if x == n+1:
                    p = p.next
                else:
                    x += 1
                h = h.next
            if length == n: return head.next
            p.next = p.next.next
            return head
    

Log in to reply
 

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