Delete the Middle Node of a Linked List

MediumLinked Listfast and slow pointers

Solve this the way you would in the interview: out loud, on a timer, with an AI interviewer asking follow-ups while you code. You get a scored report at the end.

Practice this as a mock interviewFree to start, no account needed

Problem

Given the head of a linked list with n nodes, remove its **middle** node and return the head of the modified list. The middle node is the one at 0-based index floor(n / 2): for n = 1, 2, 3, 4, 5 that is index 0, 1, 1, 2, 2. Removing the only node of a one-node list leaves an empty list []. Aim for a single pass using two pointers.

Examples

Input: head = [1,3,4,7,1,2,6]
Output: [1,3,4,1,2,6]
n = 7, so the node at index 3 (value 7) is removed.
Input: head = [1,2,3,4]
Output: [1,2,4]
n = 4, so index 2 (value 3) is removed.
Input: head = [2,1]
Output: [2]
n = 2, so index 1 (value 1) is removed.

Constraints

  • 1 <= number of nodes <= 10^5
  • 1 <= Node.val <= 10^5

What the interviewer grades

A correct solution is one of five scores. The report also grades problem understanding (did you clarify inputs and edge cases before coding), problem solving, communication while you work, code quality, and testing. See a sample report, or read up on the pattern in the lessons.

More fast and slow pointers problems