Skip to content
Onsite

Middle of the Linked List

EasyP1Coding

Given the head of a non-empty singly linked list, return the value of its middle node. If the list has an even number of nodes, there are two middle nodes: return the value of the second one.

A list node has val and next. The ListNode class is already defined for you. Aim for a single pass.

Example 1

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

Example 2

Input
head = [1,2,3,4,5,6]
Output
4
Why
Two middles (3 and 4); return the second.

Example 3

Input
head = [1]
Output
1

Constraints

  • 1 <= number of nodes <= 100
  • -100 <= Node.val <= 100

Hints

Run checks the 3 visible tests, Submit all 8.