Delete the Middle Node of a Linked List - Problem

You are given the head of a singly linked list. Your task is to delete the middle node and return the head of the modified linked list.

The middle node of a linked list of size n is defined as the ⌊n / 2⌋th node from the start using 0-based indexing, where ⌊x⌋ denotes the floor function (largest integer less than or equal to x).

Examples of middle nodes:

  • For n = 1: middle node is at index 0
  • For n = 2: middle node is at index 1
  • For n = 3: middle node is at index 1
  • For n = 4: middle node is at index 2
  • For n = 5: middle node is at index 2

Special case: If the linked list has only one node, return null (or empty list).

Input & Output

example_1.py — Basic Case
$ Input: head = [1,3,4,7,1,2,6]
Output: [1,3,4,1,2,6]
💡 Note: The linked list has 7 nodes. The middle node is at index ⌊7/2⌋ = 3 (0-indexed), which contains value 7. After removing this node, we get [1,3,4,1,2,6].
example_2.py — Even Length
$ Input: head = [1,2,3,4]
Output: [1,2,4]
💡 Note: The linked list has 4 nodes. The middle node is at index ⌊4/2⌋ = 2 (0-indexed), which contains value 3. After removing this node, we get [1,2,4].
example_3.py — Single Node
$ Input: head = [1]
Output: []
💡 Note: The linked list has only 1 node. The middle node is at index ⌊1/2⌋ = 0. After removing this node, the list becomes empty, so we return null.

Constraints

  • The number of nodes in the list is in the range [1, 105]
  • 1 ≤ Node.val ≤ 105
  • Follow-up: Can you solve this in one pass?

Visualization

Tap to expand
Linked List Race Track🐢Slow (1x)🐰Fast (2x)12345MIDDLE NODE⭐ Key Insight: When fast pointer travels 2n distance, slow pointer travels n distance⭐ Result: Slow pointer ends up exactly at the middle of the list!⭐ Time Complexity: O(n) - single pass, Space: O(1) - only using pointers
Understanding the Visualization
1
Start Both Pointers
Both tortoise (slow) and hare (fast) start at the beginning of the linked list
2
Move at Different Speeds
In each iteration: tortoise moves 1 step, hare moves 2 steps
3
Hare Reaches the End
When hare reaches the end (or goes beyond), tortoise is exactly at the middle
4
Delete Middle Node
Use the previous pointer to safely delete the middle node found by tortoise
Key Takeaway
🎯 Key Insight: The two-pointer technique elegantly finds the middle in one pass by leveraging the mathematical relationship: when the fast pointer (2x speed) reaches the end, the slow pointer (1x speed) will be exactly at the middle position!
Asked in
Amazon 45 Microsoft 38 Google 32 Meta 28
78.2K Views
High Frequency
~15 min Avg. Time
2.8K Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen