Skip to content
DeepForgeby svx
All problems
EasyData Structuresds-009

Middle Node of a Linked List

Problem

Return the value of the middle node of a singly linked list given as a list of values.

Use the slow/fast pointer idea: advance a slow pointer by one node and a fast pointer by two until the fast pointer reaches the end. For an even number of nodes, return the second middle value.

Return None for an empty list.

Hint

When the fast index passes the end, the slow index sits at the middle.

Test cases

case 1
in: [[1,2,3,4,5]]
expected: 3
case 2
in: [[1,2,3,4]]
expected: 3
case 3
in: [[1]]
expected: 1
case 4
in: [[]]
expected: null
Starter codePython
def middle_node(values):
    # Your code here
    pass

4 test cases · run your code against them in the editor