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 problem
0
Problem

Remove Nth Node From End

LeetCode #19Medium
Two pointers locked n apart

Given 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.

Asked atAmazonMetaMicrosoft
step 1 / 12
1
2
3
4
5
[0][1][2][3][4]
Brute force · two passes
1given head, n
2L = count the nodes // pass 1
3walk to node (L − n − 1) // pass 2
4cur.next = cur.next.next // splice
5return head
state
  • n2

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.