Middle of a Linked List
You are given the head of a non-empty singly linked list. Return its middle node. When the list has two middle nodes (an even number of nodes), return the second of them.
The answer is shown as the list from the middle node to the end.
Lists in the examples are written as their values from head to tail: [1,2,3] is 1 → 2 → 3, and [] is an empty list, with no head at all.
Constraints
- 1 ≤ number of nodes ≤ 5,000
- 1 ≤ node value ≤ 100
Example 1
Input
head = [1,2,3,4,5]
Output
[3,4,5]
Why:
The middle node is 3, and from it the list runs 3 → 4 → 5.
Example 2
Input
head = [1,2,3,4,5,6]
Output
[4,5,6]
Why:
3 and 4 are both in the middle; the answer is the second, 4.
Hints
Was this a good challenge?
- Solved by
- 0
- Submissions
- 0
- Accepted
- –