My accepted Python Solution - Using Fast and Slow pointers


  • 0
    S
    class Solution:
      def removeNthFromEnd(self, head, n):
         ptr1 = ptr2 = head
         count = 0
    
        while count != n:
          ptr1 = ptr1.next
          count += 1
        
        if not ptr1:
          return head.next
    
        while ptr1:
          ptr1 = ptr1.next
          temp = ptr2
          ptr2 = ptr2.next
      
        temp.next = ptr2.next
        
        return head

Log in to reply
 

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