0019. Remove Nth Node From End of List
Medium | Linked List + Two Pointer | 12 ms (98.94%), 13.3 MB (72.36%)
Last updated
Was this helpful?
Medium | Linked List + Two Pointer | 12 ms (98.94%), 13.3 MB (72.36%)
Last updated
Was this helpful?
Source: GitHub:
Given the head
of a linked list, remove the nth
node from the end of the list and return its head.
Move the fast-pointer forward to create n
gaps apart from the slow-pointer
If the fast-point now points to null, it means the first node is the one to be removed
Move forward the fast-pointer and slow-pointer together until the next node of the fast-pointer is null
Now, the next node of the slow-pointer is the one that needs to be removed. So then break the link by slowP.next = slowP.next.next
and return the head