
Sponsored
Sponsored
This approach involves converting the linked list into a circular list and then breaking it at the correct point to achieve the required rotation.
By making the linked list a circular one, you can easily traverse k nodes from the end to set the new head and make necessary detachments.
Time Complexity: O(n) because we pass through the list twice, once to find the length and once to find the new head.
Space Complexity: O(1) since we only use a constant amount of extra space.
1class ListNode:
2 def __init__(self, x):
3 self.val = x
4 self.next = None
5
6class Solution:
7 def rotateRight(self, head: ListNode, k: int) -> ListNode:
8 if not head or k == 0:
9 return head
10 # Compute the length of the list
11 length = 1
12 current = head
13 while current.next:
14 current = current.next
15 length += 1
16 current.next = head # make circular
17 k = k % length
18 # Calculate the position to break at
19 for _ in range(length - k):
20 current = current.next
21 new_head = current.next
22 current.next = None
23 return new_head
24
25# Example usage
26head = ListNode(1)
27head.next = ListNode(2)
28head.next.next = ListNode(3)
29head.next.next.next = ListNode(4)
30head.next.next.next.next = ListNode(5)
31solution = Solution()
32rotated_head = solution.rotateRight(head, 2)
33current = rotated_head
34while current:
35 print(current.val, end=' ')
36 current = current.next
37The Python solution implements the same algorithm. Traverse the list to make it circular by connecting the last node's next pointer to the head. Use the length of the list to handle cases where k is larger than the list length. Break the circle to form the rotated list.
This code also contains a usage example to demonstrate its working.
In this approach, we utilize a two-pointer technique. We advance one pointer ahead by k nodes and then move another pointer from the start concurrently until the first pointer reaches the list's end. This way, the second pointer will eventually land on the node that will become the new tail.
Time Complexity: O(n) because it requires traversal for counting.
Space Complexity: O(1) as no extra space is used.
1public class ListNode {
2 public int val;
3 public ListNode next;
4 public ListNode(int val = 0, ListNode next = null) {
this.val = val;
this.next = next;
}
}
public class Solution {
public ListNode RotateRight(ListNode head, int k) {
if (head == null || k == 0) return head;
ListNode fast = head, slow = head;
int length = 1;
while (fast.next != null) {
fast = fast.next;
length++;
}
fast.next = head; // form circular
k %= length;
for (int i = 0; i < length - k; i++) {
slow = slow.next;
fast = fast.next;
}
head = slow.next;
fast.next = null;
return head;
}
public static void Main() {
ListNode head = new ListNode(1, new ListNode(2, new ListNode(3, new ListNode(4, new ListNode(5)))));
Solution sol = new Solution();
ListNode newHead = sol.RotateRight(head, 2);
ListNode curr = newHead;
while (curr != null) {
Console.Write(curr.val + " ");
curr = curr.next;
}
}
}
In this C# solution, the logic is similar to the previous languages. After determining the length and connecting the end, the fast and slow pointers identify the correct division point. This method makes efficient use of mid-list pointer adjustments to achieve rotation.