dsa · easy

Middle of Linked List

You are given the head of a singly linked list. Each node has val and next. Return the **middle node** (the node itself). If the list has even length, return the **second** of the two middles. Empty list → null.

Arguments

Walk .next on the ListNode. Return the middle **node**; the judge prints its value.

Example

[1,2,3,4,5] → the node with value 3. [1,2,3,4] → the node with value 3 (second middle).

Constraints

0 <= length <= 100 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]

Expected:
3

Open in the Dojo editor