Problem · Linked List
Middle Node Of A Linked List
Learn this problemProblem statement
You are given the head of a singly linked list.
Return the middle node of the list. If the list has an even number of nodes, return the second of the two middle nodes.
The returned node is the start of the suffix that begins at the middle; callers observe the remaining values from that node to the end.
Function
middleNode(head: ListNode) → ListNodeExamples
Example 1
head = [1,2,3,4,5]return = [3,4,5]The list has five nodes. The middle node holds 3, so the remaining suffix is 3 -> 4 -> 5.
Example 2
head = [1,2,3,4,5,6]return = [4,5,6]The list has six nodes, so the two middle nodes hold 3 and 4. The judged answer is the second middle, 4 -> 5 -> 6.
Constraints
- The number of nodes is in the range
[1, 100]. 1 <= node.val <= 100.