Middle of the Linked List

easy

Given the head of a singly linked list, return the middle node of the list.

If the list has an even number of nodes, there are two middle nodes — return the second of the two.

You are handed the head pointer; return the node where the middle begins. Everything from that node to the end is your answer.

Hints

You can't jump to the middle by index — you can only walk forward. What's the simplest way to still know where the middle is?
One way: measure the length first, then walk half of it. Can you find the middle in a single pass instead?
Send one pointer twice as fast as the other. When the fast one reaches the end, where does the slow one sit?

Common doubts

It moves at half the speed of fast. By the time fast has covered the full length, slow has covered exactly half — the midpoint.
Loop while both fast and fast.next exist. On even lengths fast overshoots to null and slow lands one node past center — the second middle.
Return the node itself. From that node the rest of the list follows, which is exactly the sublist the problem asks for.

Interview follow-ups

The same fast/slow setup: if fast ever meets slow, there's a cycle. It's called Floyd's cycle detection — this problem is the gentle introduction to it.
Advance one pointer n steps first, then move both together until the leader hits the end — the trailer is now at the node to remove.

Fun facts

  • The fast/slow trick is the same 'tortoise and hare' idea Robert Floyd used for cycle detection in the 1960s.
  • This one pattern powers finding the middle, detecting cycles, finding a cycle's start, and locating the nth node from the end — learn it once, reuse it constantly.

Asked at

AmazonMicrosoftGoogleAdobeMeta
Frequently Sometimes Occasionally
Example 1
Input: head = [1,2,3,4,5]
Output: [3,4,5]
The list has 5 nodes, so the single middle is node 3.
Example 2
Input: head = [1,2,3,4,5,6]
Output: [4,5,6]
The list has 6 nodes, so the two middles are 3 and 4 — we return the second one, node 4.
Constraints

- The number of nodes in the list is in the range [1, 100]. - 1 <= Node.val <= 100

Solve this problem →