19. Remove Nth Node From End of List
Last updated
Was this helpful?
Last updated
Was this helpful?
Given a linked list, remove then-th node from the end of list and return its head.
Example:
Note:
Given _n _will always be valid.
Follow up:
Could you do this in one pass?
Thoughts:
dfs "backtracking": recursively determine the index, with Value-Shifting
Index and Remove: recursively determine the index, with "deleting" the nth node (selecting the next node)
Two pointer: Having fast node go n steps first then when fast reaches the end (fast.next == null); slow.next is the node to be deleted: (if after n step fast is null (means n == len(list)) then only move the head by returning head.next; else slow and fast go until fast reaches the end, and assign slow.next = slow.next.next
from: 's
Code: Value-Shifting
Code: Index and Remove
Code: two pointer: slow.next is the node to be deleted