dsa · easy

Middle of the Linked List

You are given head, the node values of a singly linked list in order from the front. Return the value of the middle node. If the list has two middle nodes, return the second one.

Arguments

The list is never empty.

Example

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

Five nodes: the middle is the third → 3.

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

Six nodes: two middles (3 and 4); return the second → 4.

Constraints

1 <= head.length <= 100 1 <= node value <= 10^5 Hidden tests include near-max size for this bound; a slower-than-intended solution TLEs.

Examples

Example 1

Input:
[1,2,3,4,5]

Expected:
3

Example 2

Input:
[1,2,3,4,5,6]

Expected:
4

Example 3

Input:
[1]

Expected:
1

Open in the Dojo editor