Coding Interview PatternsMiddle of the Linked List
EasyFast and Slow Pointers

Middle of the Linked List

Explanation & Solution

Description

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

If there are two middle nodes, return the second middle node.

The result should be returned as the sublist from the middle node to the end of the list.

Input:head = [1,2,3,4,5]
0
1
1
2
2
3
3
4
4
5
Output:[3,4,5]
0
3
1
4
2
5

Explanation: The middle node of the list is node 3.

Constraints

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

Approach

Fast and Slow Pointers pattern

1. Initialize Two Pointers

  • Set slow to head
  • Set fast to head
  • Both pointers start at the beginning of the linked list

2. Traverse the List

  • Loop while fast is not null and fast.next is not null
  • Move slow forward by one step: slow = slow.next
  • Move fast forward by two steps: fast = fast.next.next

3. Return the Middle

  • When fast reaches the end (or goes past it), slow is at the middle node
  • For odd-length lists, slow is exactly at the center
  • For even-length lists, slow is at the second of the two middle nodes
  • Collect all values from slow to the end of the list into an array and return it

4. Why Does This Work?

  • Since fast moves at twice the speed of slow, when fast has traversed the entire list, slow has traversed exactly half
  • This is a classic application of the fast-and-slow pointer (tortoise and hare) technique

Key Insight

  • No need to count the length of the list first — the two-pointer approach finds the middle in a single pass
Time
O(n)we traverse the list once
Space
O(1)only two pointer variables are used (excluding the output array)

Visualization

Input:
Linked List Traversal
1021324354null

Press play to start traversal

Slow (S)Fast (F)Both
5 steps

Solution Code