Middle of the Linked List

Linked Lists, problem 3 of 8

Middle of the Linked List

Easy

LC #876

fast & slow pointers

Not attempted yet

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

The result is the list starting at that node.

Example 1

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

Example 2

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

Example 3

Input: head = [9]
Output: [9]

Constraints

  • 1 ≤ number of nodes ≤ 10⁵
  • 1 ≤ Node.val ≤ 10⁵

Python

Loading draft…

Test results

7 tests available

No results yet

Run tests your code against the examples; Submit runs the hidden tests too.

3 examples, 4 hidden

Run examples, then submit all tests.