Delete the Middle Node of a Linked List - Problem
You are given the head of a linked list. Delete the middle node, and return the head of the modified linked list.
The middle node of a linked list of size n is the ⌊n / 2⌋th node from the start using 0-based indexing, where ⌊x⌋ denotes the largest integer less than or equal to x.
For n = 1, 2, 3, 4, and 5, the middle nodes are 0, 1, 1, 2, and 2, respectively.
Input & Output
Example 1 — Odd Length List
$
Input:
head = [1,3,4,7,1,2,6]
›
Output:
[1,3,4,1,2,6]
💡 Note:
List has 7 nodes. Middle is at index ⌊7/2⌋ = 3 (value 7). After deletion: [1,3,4,1,2,6]
Example 2 — Even Length List
$
Input:
head = [1,2,3,4]
›
Output:
[1,2,4]
💡 Note:
List has 4 nodes. Middle is at index ⌊4/2⌋ = 2 (value 3). After deletion: [1,2,4]
Example 3 — Two Nodes
$
Input:
head = [2,1]
›
Output:
[1]
💡 Note:
List has 2 nodes. Middle is at index ⌊2/2⌋ = 1 (value 1). After deletion: [1] - wait, that's wrong. Middle should be index 1, so delete node with value 1, leaving [2]. Actually, for n=2, middle index is 1, so we delete the second node, leaving [2].
Constraints
- The number of nodes in the list is in the range [1, 105]
- 1 ≤ Node.val ≤ 105
Visualization
Tap to expand
Understanding the Visualization
1
Input List
Original linked list [1,3,4,7,1,2,6] with 7 nodes
2
Find Middle
Middle position is ⌊7/2⌋ = 3, which contains value 7
3
Delete Middle
Remove middle node, resulting in [1,3,4,1,2,6]
Key Takeaway
🎯 Key Insight: Use fast-slow pointers to find the middle node in one pass - when fast reaches the end, slow is at the right position to delete the middle
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code