Remove Nth Node From End of List - Complete Solution Guide
Remove Nth Node From End of List is LeetCode problem 19, a Medium 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 linked list, remove the n th node from the end of the list and return its head. Example 1: Input: head = [1,2,3,4,5], n = 2 Output: [1,2,3,5] Example 2: Input: head = [1], n = 1 Output: [] Example 3: Input: head = [1,2], n = 1 Output: [1] Constraints: The number of nodes in the list is sz . 1 <= sz <= 30 0 <= Node.val <= 100 1 <= n <= sz Follow up: Could you do this in one pass?
Detailed Explanation
The problem requires us to remove the nth node from the end of a given singly linked list and return the modified list's head. The input is the head of the linked list and an integer 'n'. The output is the head of the updated linked list with the nth node from the end removed. The constraint is that 'n' will always be valid, meaning it will be within the bounds of the list's size. We need to accomplish this efficiently and ideally in one pass.
Solution Approach
The solution uses the 'two-pointer' technique (also known as 'fast and slow pointers'). A 'fast' pointer moves 'n' steps ahead of the 'slow' pointer. Then, both pointers move forward simultaneously until the 'fast' pointer reaches the end of the list. At this point, the 'slow' pointer will be pointing to the node *before* the nth node from the end, allowing us to easily remove the nth node. A dummy node is used at the beginning of the list to handle the cases where the head of the list needs to be removed.
Step-by-Step Algorithm
- Step 1: Create a dummy node and set its 'next' pointer to the head of the linked list. This simplifies removing the head node if necessary.
- Step 2: Initialize two pointers, 'slow' and 'fast', both pointing to the dummy node.
- Step 3: Move the 'fast' pointer 'n' steps forward. Now, the 'fast' pointer is 'n' nodes ahead of the 'slow' pointer.
- Step 4: Move both 'slow' and 'fast' pointers one step at a time until the 'fast' pointer reaches the end of the list (fast.next is null).
- Step 5: At this point, the 'slow' pointer is pointing to the node immediately before the nth node from the end.
- Step 6: Remove the nth node from the end by updating the 'next' pointer of the 'slow' node: `slow.next = slow.next.next`.
- Step 7: Return the 'next' pointer of the dummy node, which is the head of the modified linked list.
Key Insights
- Insight 1: We need to find the nth node from the end without knowing the length of the list beforehand.
- Insight 2: Using two pointers with a fixed gap can help identify the node to be removed in a single pass.
- Insight 3: Handling the edge case where the head itself needs to be removed is crucial. A dummy node simplifies this.
Complexity Analysis
Time Complexity: O(n)
Space Complexity: O(1)
Topics
This problem involves: Linked List, Two Pointers.
Companies
Asked at: Adobe, Amazon, Apple, Bloomberg, Citrix, Meta, Microsoft, Nvidia, Qualcomm, Uber, Yahoo.