40ms python solution


  • 0
    M
    def removeNthFromEnd(self, head, n):
        counter = 0
        ptr,lagPtr = 0,0
        ptr = head
        lagPtr = head
        while ptr != None:
            ptr = ptr.next
            if counter > n :
                lagPtr = lagPtr.next
            counter += 1
        if counter - n > 0:
            #remove
            lagPtr.next = lagPtr.next.next
            return head
        if counter - n == 0:
            return head.next
        else:
            return None

  • 0
    L

    I tried your algorithm, but the run time is 73ms


Log in to reply
 

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