Delete the Middle Node of a Linked List

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 floor(n / 2)th node from the start using 0-based indexing. For n = 1, 2, 3, 4, and 5, the middle nodes are 0, 1, 1, 2, and 2, respectively.

30:00

Delete the Middle Node of a Linked List
medium
Free written solution
Topics
Companies

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 floor(n / 2)th node from the start using 0-based indexing. For n = 1, 2, 3, 4, and 5, the middle nodes are 0, 1, 1, 2, and 2, respectively.

Example 1:
Input: {"head":[1,3,4,7,1,2,6]}
Output: [1,3,4,1,2,6]
Constraints:
  • The number of nodes in the list is in the range [1,105][1, 10^5].

  • 1≤Node.val≤1051 \leq \text{Node.val} \leq 10^5

Test Cases
Input (Stdin)
Expected Output (Stdout)
Execution Result

Run code to see results.

Input
arr ={"head":[1,3,4,7,1,2,6]}

The list has 7 nodes, so the middle is index floor(7 / 2) = 3. To unlink it we need the node just before it.

▼ fast: 2 steps▲ slow: 1 step■ middle = index ⌊n/2⌋ = 3
null10314273142566

Distance covered

slow: 0

fast: -

Reading the list from head

[1, 3, 4, 7, 1, 2, 6]

Variables
VariableValue
n7
middle3
slow1
fastnull
DepthFunction Call
Stack empty
0/11