Middle of a Linked List
A singly linked list is given to you as an array values, where values[0] is the head and each element points to the next one.
Return the part of the list that starts at the middle node, as an array. When the list has an even number of nodes there are two middle nodes; start from the second one.
Try solving it with a slow and a fast pointer, as you would on a real linked list where the length is not known in advance.
Example 1
Input: values = [1,2,3,4,5] Output: [3,4,5] Explanation: The middle node holds 3.
Example 2
Input: values = [1,2,3,4,5,6] Output: [4,5,6] Explanation: The two middle nodes are 3 and 4; we start from the second.
Constraints
- 0 <= values.length <= 10^4
- -10^5 <= values[i] <= 10^5
Hint 1
Move one pointer one step at a time and another two steps at a time.
Hint 2
When the fast pointer runs off the end, the slow pointer is at the middle.
Good practice for: Backend Developer, Full Stack Developer, Frontend Developer, Embedded Systems Engineer, Game Developer
Run checks your code on 3 sample tests. Submit runs all 7 tests, including hidden ones, and earns points. Use console.log to debug. Python's first run can take a few seconds while it starts.