1
2
3
4
5
[0][1][2][3][4]
Brute force · two passes
▸1given head, n2L = count the nodes // pass 13walk to node (L − n − 1) // pass 24cur.next = cur.next.next // splice5return head
state
- n2
Your free access ends in 7 days — and you haven’t tried it yet. Watch one algorithm run, start to finish. It takes about two minutes.
Try one problemGiven the head of a singly linked list, remove the nth node counting from the end of the list and return the head of the modified list.
▸1given head, n2L = count the nodes // pass 13walk to node (L − n − 1) // pass 24cur.next = cur.next.next // splice5return head
line 1Remove the 2nd node FROM THE END. Awkward: lists only walk forward, and we don't know the length. Two-pass fix: pass 1 counts, pass 2 walks.