my short python solution in O(n) time & O(1) space


  • 2
    G
    class Solution(object):
        def removeNthFromEnd(self, head, n):
            """
            :type head: ListNode
            :type n: int
            :rtype: ListNode
            """
            dummy = ListNode(0)
            dummy.next = head
            first, second = dummy, dummy
            for i in xrange(n):
                first = first.next
            while first.next:
                first = first.next
                second = second.next
            second.next = second.next.next
            return dummy.next
    

Log in to reply
 

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