52ms python solution


  • 0
    F
    # 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):
            if head.next==None:
                return None
            Node=head
            flag=n
            while True:
                myNode=Node
                while n>1:
                    myNode=myNode.next
                    n-=1
                if myNode.next==None:
                    head=head.next
                    return head
                if myNode.next.next==None:
                    break
                Node=Node.next
                n=flag
            Node.next=Node.next.next
            return head

Log in to reply
 

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