InterviewPrepKit

Home / Coding / Linked List

Delete the Middle Node of a Linked List

medium Original β†—
Solving tips
  • To delete from a singly linked list you must stand on the node BEFORE the target, so aim slow at index floor(n/2) - 1, not the middle itself.
  • Single pass with fast/slow pointers: give fast a head start (e.g. start at head.next.next, or use a dummy so slow starts one behind) so slow lands just before the middle.
  • Handle the 1-node list explicitly (return None) or use a dummy node so it falls out of the general code.
  • Target O(n) time, O(1) space; check fast and fast.next in the loop to avoid None.next, and verify off-by-one by hand on n = 2 and n = 4.

Problem

Given the head of a singly linked list with n nodes, remove the middle node and return the head of the modified list.

The middle node is the one at index ⌊n / 2βŒ‹ (0-based, counting from the head). So for a list of 1 node you delete the head itself (returning an empty list); for 2 nodes you delete the second; for 7 nodes you delete index 3.

Examples

  • Input: 1 -> 3 -> 4 -> 7 -> 1 -> 2 -> 6 β†’ Output: 1 -> 3 -> 4 -> 1 -> 2 -> 6 n = 7, so the node at index ⌊7/2βŒ‹ = 3 (value 7) is removed.
  • Input: 1 -> 2 -> 3 -> 4 β†’ Output: 1 -> 2 -> 4 n = 4, index ⌊4/2βŒ‹ = 2 (value 3) goes.
  • Input: 2 -> 1 β†’ Output: 2 n = 2, index 1 (value 1) is deleted.

Constraints

  • Number of nodes is in [1, 10^5].
  • 1 <= Node.val <= 10^5

Think about it first

Hint 1 To delete a node from a singly linked list you must be standing on the node before it. Which index is that here?
Hint 2 Two passes work: count n, then walk to index ⌊n/2βŒ‹ βˆ’ 1 and bypass the next node. Can you find the middle in a single pass instead?
Hint 3 Run a slow and a fast pointer (1 step vs. 2 steps). When fast reaches the end, slow is at the middle β€” so if you start slow one node behind (or offset fast), slow lands on the node just before the middle, ready to unlink it.
Your workspace Not runnable by design β€” this is your interview scratchpad. Saved on this device.