Rotate List - Complete Solution Guide
Rotate List is LeetCode problem 61, 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, rotate the list to the right by k places. Example 1: Input: head = [1,2,3,4,5], k = 2 Output: [4,5,1,2,3] Example 2: Input: head = [0,1,2], k = 4 Output: [2,0,1] Constraints: The number of nodes in the list is in the range [0, 500] . -100 <= Node.val <= 100 0 <= k <= 2 * 10 9
Detailed Explanation
The problem requires us to rotate a singly linked list to the right by `k` places, where `k` is a non-negative integer. Rotating a list to the right means moving the last `k` elements to the beginning of the list and shifting the remaining elements to the right. If `k` is larger than the length of the list, we effectively rotate by `k % length`.
Solution Approach
The solution first calculates the length of the linked list. Then, it calculates the effective number of rotations by taking `k` modulo the length. After the effective rotation amount is determined, we connect the tail of the original list to the head to form a circular linked list. Then, we traverse the list to find the new tail, which will be `length - k - 1` nodes from the original head. Finally, we break the connection between the new tail and the node after it, making the node after the new tail as the new head.
Step-by-Step Algorithm
- Step 1: Check if the linked list is empty or has only one node, or if k is zero. If so, return the original list.
- Step 2: Calculate the length of the linked list by traversing it once. Also, save the last node as `tail`.
- Step 3: Calculate the effective number of rotations by taking `k` modulo the length: `k = k % length`. If `k` becomes 0 after the modulo, it means no rotation is required, so return the original list.
- Step 4: Connect the tail of the linked list to the head to form a circular linked list: `tail.next = head`.
- Step 5: Calculate the number of steps needed to reach the new tail: `steps_to_new_tail = length - k - 1`.
- Step 6: Traverse `steps_to_new_tail` nodes from the original head to find the new tail.
- Step 7: The node after the new tail will be the new head: `new_head = new_tail.next`.
- Step 8: Break the connection between the new tail and the new head: `new_tail.next = null`.
- Step 9: Return the new head.
Key Insights
- Insight 1: We need to find the length of the linked list to handle cases where 'k' is larger than the length of the list. This is achieved by traversing the list once.
- Insight 2: The actual rotation happens by finding the new tail and new head after the rotation. We calculate where the new tail should be located and break the list into two segments to form the rotated list.
- Insight 3: Handle edge cases such as an empty list, a list with only one node, or k equals to zero as they will result in the original list as the output.
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, LinkedIn, Meta, Microsoft, Morgan Stanley, Oracle, Siemens, Yahoo.