19. Remove Nth Node From End of List
At a Glance
- Topic: linked-list
- Pattern: Fast & Slow Pointers (gap / offset)
- Difficulty: Medium
- Companies: Amazon, Google, Meta, Apple, Adobe
- Frequency: Very High
- LeetCode: 19
Problem (one-liner)
Given a singly linked list and nthFromEnd, remove the node that is nthFromEnd steps from the tail and return the head (first node may be removed).
Recognition Cues
- “Nth from end” with one pass requirement
- Fast pointer leads slow by
nthFromEndgaps - Dummy head simplifies removing the first node
Diagram
At-a-glance flow (replace with problem-specific Mermaid as you refine this note). camelCase node IDs; no spaces in IDs.
Loading diagram…
Approaches
- Brute force — two passes: measure length
length, remove at indexlength - nthFromEnd. - Better — one pass with array of pointers —
O(n)space. - Optimal —
O(n)time /O(1)space — advancefastbynthFromEnd+1from dummy, then move both untilfasthits end;slow.Nextis the node to skip.
Optimal Solution
Go
type ListNode struct {
Val int
Next *ListNode
}
func removeNthFromEnd(head *ListNode, nthFromEnd int) *ListNode {
dummy := &ListNode{Next: head}
lead := dummy
for step := 0; step <= nthFromEnd; step++ {
lead = lead.Next
}
trail := dummy
for lead != nil {
lead = lead.Next
trail = trail.Next
}
trail.Next = trail.Next.Next
return dummy.Next
}JavaScript
class ListNode {
constructor(val = 0, next = null) {
this.val = val;
this.next = next;
}
}
function removeNthFromEnd(head, nthFromEnd) {
const dummy = new ListNode(0, head);
let lead = dummy;
for (let step = 0; step <= nthFromEnd; step++) {
lead = lead.next;
}
let trail = dummy;
while (lead !== null) {
lead = lead.next;
trail = trail.next;
}
trail.next = trail.next.next;
return dummy.next;
}Walkthrough
head = [1,2,3,4,5], nthFromEnd = 2 → remove value 4.
| step | lead position after advancing | trail stops before |
|---|---|---|
| init | dummy + n+1 nodes ahead | node before target |
When lead is nil, trail.Next is the 4 node; unlink it.
Edge Cases
- Remove first node — dummy absorbs head change.
- Single node list with
nthFromEnd = 1. nthFromEndequals list length — remove head.
Pitfalls
- Off-by-one in how many steps
leadstarts ahead (needsn+1from dummy to stop before target). - Not returning
dummy.Nextafter mutating.
Similar Problems
- 141. Linked List Cycle — fast/slow traversal discipline.
- 143. Reorder List — slow/fast finds middle before further ops.
- 206. Reverse Linked List — dummy-node patterns pair well with boundary edits.
Variants
- Remove all nodes matching value from end rule — different unlink criteria.
- Doubly linked list — find node with one pass from tail pointer.
Mind-Map Tags
#linked-list #two-pointer #dummy-head #nth-from-end
Last updated on
Spotted something unclear or wrong on this page?