Menu

Sign in to track your progress and unlock all features.

Theme style

Log in

Full lesson preview

Find the middle node

Find the middle node of a singly linked list using the fast and slow pointer technique.

Python practice15 minLinked ListsIntermediateLast updated March 27, 2026

Problem statement

Given the head of a singly linked list, return the middle node. If there are two middle nodes (even length), return the second one. If the list is empty, return None. Aim to solve in O(n) time and O(1) extra space using the two-pointer technique.

Task

Return the middle node of a singly linked list. If the list has an even number of nodes, return the second middle node.

Examples

Odd length list

Input

head = [1,2,3,4,5]

Output

3

The middle of [1,2,3,4,5] is 3.

Even length list

Input

head = [1,2,3,4]

Output

3

For even length, return the second middle (3).

Input format

A linked list represented by its head node. Use create_linked_list([...]) to build inputs.

Output format

Return the middle node (ListNode). Tests will often access the .val of the returned node. Return None for an empty list.

Constraints

- The list length can be 0 or more. - Use O(1) extra space. - Time complexity should be O(n).

Samples

Sample 1

Input

[10, 20, 30, 40, 50]

Output

30

The middle node's value is 30.