Skip to content

Middle of the Linked List

Solve on LeetCodeEasy
Time
Space

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.

Example 1:

The list 1→2→3→4→5 with node 3 highlighted as the middle

Input: head = [1,2,3,4,5]
Output: [3,4,5]
Explanation: The middle node of the list is node 3.

Example 2:

The list 1→2→3→4→5→6 with node 4 highlighted as the second middle

Input: head = [1,2,3,4,5,6]
Output: [4,5,6]
Explanation: Since the list has two middle nodes with values 3 and 4, we return the second one.

Constraints:

  • The number of nodes in the list is in the range [1, 100].
  • 1≤Node.val≤1001 \le \text{Node.val} \le 100

Counting the nodes and then walking count / 2 steps works, but needs two passes. We can do it in one with the slow and fast pointer (tortoise and hare) technique: slow moves one node per step and fast moves two. When fast runs off the end of the list, slow has covered exactly half the distance, so it is standing on the middle.

Invariant: after k steps, slow is node k and fast is node 2k (0-indexed). The loop stops at the first k for which node 2k or node 2k + 1 does not exist, i.e. k = floor(n / 2) — which is the middle for odd n and the second middle for even n, exactly what the problem asks.

  1. Set slow = fast = head.
  2. While fast and fast->next both exist: slow = slow->next, fast = fast->next->next.
  3. Return slow.

Example 2, with slow in blue and fast in orange (the trailing null is where fast falls off the list):

  1. slow and fast pointers on 1→2→3→4→5→6, step 1 of 5
  2. slow and fast pointers on 1→2→3→4→5→6, step 2 of 5
  3. slow and fast pointers on 1→2→3→4→5→6, step 3 of 5
  4. slow and fast pointers on 1→2→3→4→5→6, step 4 of 5
  5. slow and fast pointers on 1→2→3→4→5→6, step 5 of 5
5 steps. Swipe or scroll to move between them.
  • Time: O(n)O(n) — fast visits about every second node, so the loop runs ⌊n/2⌋\lfloor n/2 \rfloor times in a single pass.
  • Space: O(1)O(1) — two pointers.
  • Single node: fast->next is null, the loop never runs and head is returned.
  • Even length: the loop stops when fast becomes null (not when fast->next is null), which leaves slow on the second middle, as in Example 2.

Think before you peek

Work out an approach on your own first — the explanation will still be here when you want it.

class Solution {
public:
ListNode* middleNode(ListNode* head) {
ListNode* slow = head;
ListNode* fast = head;
while (fast && fast->next) {
slow = slow->next;
fast = fast->next->next;
}
return slow;
}
};
class Solution {
public ListNode middleNode(ListNode head) {
ListNode slow = head;
ListNode fast = head;
while (fast != null && fast.next != null) {
slow = slow.next;
fast = fast.next.next;
}
return slow;
}
}
class Solution:
def middleNode(self, head: ListNode | None) -> ListNode | None:
slow = fast = head
while fast and fast.next:
slow = slow.next
fast = fast.next.next
return slow

Give it a try first

Attempt the problem on LeetCode — the code will still be here.