You are given head, the first node of a singly linked list.
Return the middle node of the list, as the list that starts at that node. If the list has two middle nodes, return the list that starts at the second one.
Linked List Encoding
Your function receives head as a ListNode and returns the node where the result list starts. The node type is provided for you, with val and next fields.
The examples below write a list as the array of its values in order, so [1,2,3] is the list 1 -> 2 -> 3. That is only how lists are displayed; the encoding and decoding are done for you.
Examples
Example 1
Input: head = [1,2,3,4,5]
Output: [3,4,5]
Explanation: The list has one middle node, with value 3. The result is the list starting there.
Example 2
Input: head = [1,2,3,4,5,6]
Output: [4,5,6]
Explanation: The list has two middle nodes, with values 3 and 4. The result is the list starting at the second one.
Constraints
The number of nodes in the list is in the range [1,100].
1≤Node.val≤100
Loading editor...
Run checks the sample cases; Submit runs every case.
Samples 2
Custom 0
passedwrong answertime limiterrorran, no expected valuenot run