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
Delete the Middle Node of a Linked List INPUT Linked List (7 nodes) 1 3 4 7 1 2 6 MIDDLE Index: 0 1 2 3 4 5 6 n=7, middle = floor(7/2) = 3 head = [1, 3, 4, 7, 1, 2, 6] Use Two Pointers: slow (1 step), fast (2 steps) ALGORITHM STEPS 1 Initialize Pointers slow = head, fast = head prev = null 2 Move Fast and Slow while fast and fast.next: prev = slow slow += 1, fast += 2 3 Find Middle When fast reaches end, slow is at middle node 4 Delete Middle prev.next = slow.next Skip middle node Final Pointer Positions: 4 prev 7 slow 1 bypass FINAL RESULT Modified Linked List (6 nodes) 1 3 4 1 2 6 Node 7 deleted (index 3) Output = [1, 3, 4, 1, 2, 6] Complexity Analysis Time: O(n) - single pass Space: O(1) - constant OK Key Insight: The Fast and Slow Pointer technique (Floyd's Algorithm) finds the middle in one pass. When fast pointer reaches the end, slow pointer is at the middle. Track the previous node to bypass the middle node by setting prev.next = slow.next, effectively deleting it. TutorialsPoint - Delete the Middle Node of a Linked List | Optimal Solution (Two Pointers)
Asked in
Microsoft 25 Amazon 18 Google 15 Meta 12
145.6K Views
Medium 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