Python solution 48 ms


  • 0
    H

    Definition for singly-linked list.

    # class ListNode:
    #     def __init__(self, x):
    #         self.val = x
    #         self.next = None
    class Solution:
        # @param {ListNode} head
        # @param {integer} n
        # @return {ListNode}
        def removeNthFromEnd(self, head, n):
            curr=head
            prev=None
            temp=head
            for i in range(n):
                temp=temp.next
            cand=temp==None
            while not cand:
                temp=temp.next
                cand=temp==None
                prev=curr
                curr=curr.next
            if curr==head:
                head=head.next
            else:
                prev.next=curr.next
            return head

Log in to reply
 

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