Middle of the Linked List - Complete Solution Guide
Middle of the Linked List is LeetCode problem 876, a Easy level challenge. This complete guide provides step-by-step explanations, multiple solution approaches, and optimized code in python3, java, cpp, c.
Problem Statement
Given the head of a singly linked list, return the middle node of the linked list . If there are two middle nodes, return the second middle node. Example 1: Input: head = [1,2,3,4,5] Output: [3,4,5] Explanation: The middle node of the list is node 3. Example 2: Input: head = [1,2,3,4,5,6] Output: [4,5,6] Explanation: Since the list has two middle nodes with values 3 and 4, we return the second one. Constraints: The number of nodes in the list is in the range [1, 100] . 1 <= Node.val <= 100
Detailed Explanation
The problem asks us to find the middle node of a given singly linked list. If the list has an odd number of nodes, there will be only one middle node. If the list has an even number of nodes, there will be two middle nodes, and we need to return the *second* one. For example, in the list [1, 2, 3, 4, 5], the middle node is 3. In the list [1, 2, 3, 4, 5, 6], the middle nodes are 3 and 4, and we return 4.
Solution Approach
The provided solutions all use the "fast and slow pointer" technique (also known as the "tortoise and hare" algorithm). We initialize two pointers, `slow` and `fast`, both pointing to the head of the linked list. The `slow` pointer moves one node at a time, while the `fast` pointer moves two nodes at a time. This means the `fast` pointer traverses the list twice as fast as the `slow` pointer. The loop continues as long as the `fast` pointer and the node after it are not null. When the `fast` pointer reaches the end of the list (or the node before the end if the list has an even number of nodes), the `slow` pointer will be pointing to the middle node (or the second middle node in case of even length).
Step-by-Step Algorithm
- Step 1: Initialize two pointers, `slow` and `fast`, to the head of the linked list.
- Step 2: Iterate through the linked list using a `while` loop. The loop continues as long as `fast` is not null and `fast.next` is not null.
- Step 3: Inside the loop, move the `slow` pointer one node forward (`slow = slow.next`).
- Step 4: Also inside the loop, move the `fast` pointer two nodes forward (`fast = fast.next.next`).
- Step 5: After the loop terminates, the `slow` pointer will be pointing to the middle node (or the second middle node if the list has an even length).
- Step 6: Return the `slow` pointer.
Key Insights
- Insight 1: The 'fast and slow pointer' technique is efficient for solving linked list problems involving finding the middle or detecting cycles.
- Insight 2: The fast pointer moves twice as fast as the slow pointer. When the fast pointer reaches the end of the list, the slow pointer will be at the middle.
- Insight 3: Properly handling the edge cases where the list is empty or contains only one node is crucial for a robust solution.
Complexity Analysis
Time Complexity: O(n)
Space Complexity: O(1)
Topics
This problem involves: Linked List, Two Pointers.
Companies
Asked at: Bosch, Intuit, Qualcomm, Walmart Labs, Zoho.