Middle of the Linked List
Given the head
Example 1
Input: head = [1, 2, 3, 4, 5]
Output: [3, 4, 5]
Explanation: The middle node has value 3. Returning node 3 and everything after it.
Output: [3, 4, 5]
Explanation: The middle node has value 3. Returning node 3 and everything after it.
Example 2
Input: head = [1, 2, 3, 4, 5, 6]
Output: [4, 5, 6]
Explanation: With 6 nodes there are two middle nodes (3 and 4). The second middle node (value 4) is returned.
Output: [4, 5, 6]
Explanation: With 6 nodes there are two middle nodes (3 and 4). The second middle node (value 4) is returned.
Solution Approach - Slow and Fast Pointer
The classic approach is to use two pointers where slowfastfastslow
- Initialize both
andslow atfast .head - While
andfast != null , advancefast.next != null by 1 andslow by 2.fast - When the loop ends,
is at the middle node. For even-length lists, this naturally gives the second middle.slow - Return
.slow
Java
class ListNode {
int val;
ListNode next;
ListNode(int val) { this.val = val; }
}
public class MiddleOfLinkedList {
public ListNode middleNode(ListNode head) {
ListNode slow = head;
ListNode fast = head;
while (fast != null && fast.next != null) {
slow = slow.next; // move 1 step
fast = fast.next.next; // move 2 steps
}
return slow; // slow is at the middle
}
}
Complexity Analysis:
Time complexity: O(n) where n is the number of nodes. The fast pointer traverses the list in n/2 iterations.
Space complexity: O(1) since only two pointer variables are used.