|
| 1 | +--- |
| 2 | +id: rotate-list |
| 3 | +title: Rotate List (LeetCode) |
| 4 | +sidebar_label: 0061-rotate-list |
| 5 | +tags: |
| 6 | + - Linked List |
| 7 | + - Two Pointers |
| 8 | +description: "The rotate list is the problem to rotate the list to the right by k places." |
| 9 | +--- |
| 10 | + |
| 11 | +### Problem Description |
| 12 | + |
| 13 | +Given the `head` of a linked list, rotate the list to the right by `k` places. |
| 14 | + |
| 15 | +### Examples |
| 16 | + |
| 17 | +#### Example 1 |
| 18 | + |
| 19 | +```plaintext |
| 20 | +Input: head = [1,2,3,4,5], k = 2 |
| 21 | +Output: [4,5,1,2,3] |
| 22 | +``` |
| 23 | + |
| 24 | +#### Example 2 |
| 25 | + |
| 26 | +```plaintext |
| 27 | +Input: head = [0,1,2], k = 4 |
| 28 | +Output: [2,0,1] |
| 29 | +``` |
| 30 | + |
| 31 | +### Constraints |
| 32 | + |
| 33 | +- The number of nodes in the list is in the range `[0, 500]`. |
| 34 | +- `-100 <= Node.val <= 100` |
| 35 | +- `0 <= k <= 2 * 109` |
| 36 | + |
| 37 | +### Approach |
| 38 | + |
| 39 | +Brute Force Approach : We have to move the last element to first for each k. |
| 40 | +For each k, find the last element from the list. Move it to the first. |
| 41 | + |
| 42 | +Let’s take an example. |
| 43 | + |
| 44 | +head = [1,2,3,4,5] k = 2000000000 |
| 45 | + |
| 46 | +If we see a brute force approach, it will take O(5*2000000000) which is not a good time complexity when we can optimize it. |
| 47 | + |
| 48 | +We can see that for every k which is multiple of the length of the list, we get back the original list. Try to operate brute force on any linked list for k as a multiple of the length of the list. |
| 49 | + |
| 50 | +This gives us a hint that for k greater than the length of the list, we have to rotate the list for k%length of the list. This reduces our time complexity. |
| 51 | + |
| 52 | +Steps to the algorithm:- |
| 53 | + |
| 54 | +1. Calculate the length of the list. |
| 55 | +2. Connect the last node to the first node, converting it to a circular linked list. |
| 56 | +3. Iterate to cut the link of the last node and start a node of k%length of the list rotated list. |
| 57 | + |
| 58 | +### Solution Code |
| 59 | + |
| 60 | +#### Python |
| 61 | + |
| 62 | +``` |
| 63 | +class Node: |
| 64 | + def __init__(self, val): |
| 65 | + self.val = val |
| 66 | + self.next = None |
| 67 | +
|
| 68 | +# utility function to insert node at the end of the linked list |
| 69 | +def insertNode(head, val): |
| 70 | + newNode = Node(val) |
| 71 | + if head == None: |
| 72 | + head = newNode |
| 73 | + return head |
| 74 | + temp = head |
| 75 | + while temp.next != None: |
| 76 | + temp = temp.next |
| 77 | + temp.next = newNode |
| 78 | + return head |
| 79 | +
|
| 80 | +# utility function to rotate list by k times |
| 81 | +def rotateRight(head, k): |
| 82 | + if head == None or head.next == None or k == 0: |
| 83 | + return head |
| 84 | + # calculating length |
| 85 | + temp = head |
| 86 | + length = 1 |
| 87 | + while temp.next != None: |
| 88 | + length += 1 |
| 89 | + temp = temp.next |
| 90 | + # link last node to first node |
| 91 | + temp.next = head |
| 92 | + k = k % length # when k is more than length of list |
| 93 | + end = length - k # to get end of the list |
| 94 | + while end: |
| 95 | + temp = temp.next |
| 96 | + end -= 1 |
| 97 | + # breaking last node link and pointing to NULL |
| 98 | + head = temp.next |
| 99 | + temp.next = None |
| 100 | +
|
| 101 | + return head |
| 102 | +
|
| 103 | +# utility function to print list |
| 104 | +def printList(head): |
| 105 | + while head.next != None: |
| 106 | + print(head.val, end='->') |
| 107 | + head = head.next |
| 108 | + print(head.val) |
| 109 | + return |
| 110 | +
|
| 111 | +if __name__ == '__main__': |
| 112 | + head = None |
| 113 | + # inserting Node |
| 114 | + head = insertNode(head, 1) |
| 115 | + head = insertNode(head, 2) |
| 116 | + head = insertNode(head, 3) |
| 117 | + head = insertNode(head, 4) |
| 118 | + head = insertNode(head, 5) |
| 119 | +
|
| 120 | + print("Original list: ", end='') |
| 121 | + printList(head) |
| 122 | +
|
| 123 | + k = 2 |
| 124 | + # calling function for rotating right of the nodes by k times |
| 125 | + newHead = rotateRight(head, k) |
| 126 | +
|
| 127 | + print("After", k, "iterations: ", end='') |
| 128 | + printList(newHead) # list after rotating nodes |
| 129 | +``` |
| 130 | + |
| 131 | +#### Java |
| 132 | +``` |
| 133 | +//utility function to insert node at the end of the list |
| 134 | +static Node insertNode(Node head,int val) { |
| 135 | + Node newNode = new Node(val); |
| 136 | + if(head == null) { |
| 137 | + head = newNode; |
| 138 | + return head; |
| 139 | + } |
| 140 | + Node temp = head; |
| 141 | + while(temp.next != null) temp = temp.next; |
| 142 | + |
| 143 | + temp.next = newNode; |
| 144 | + return head; |
| 145 | +} |
| 146 | +//utility function to rotate list by k times |
| 147 | +static Node rotateRight(Node head,int k) { |
| 148 | + if(head == null||head.next == null||k == 0) return head; |
| 149 | + //calculating length |
| 150 | + Node temp = head; |
| 151 | + int length = 1; |
| 152 | + while(temp.next != null) { |
| 153 | + ++length; |
| 154 | + temp = temp.next; |
| 155 | + } |
| 156 | + //link last node to first node |
| 157 | + temp.next = head; |
| 158 | + k = k%length; //when k is more than length of list |
| 159 | + int end = length-k; //to get end of the list |
| 160 | + while(end--!=0) temp = temp.next; |
| 161 | + //breaking last node link and pointing to NULL |
| 162 | + head = temp.next; |
| 163 | + temp.next = null; |
| 164 | + |
| 165 | + return head; |
| 166 | +} |
| 167 | +
|
| 168 | +//utility function to print list |
| 169 | +static void printList(Node head) { |
| 170 | + while(head.next != null) { |
| 171 | + System.out.print(head.num+"->"); |
| 172 | + head = head.next; |
| 173 | + } |
| 174 | + System.out.println(head.num); |
| 175 | + |
| 176 | +} |
| 177 | +``` |
| 178 | + |
| 179 | +#### C++ |
| 180 | +``` |
| 181 | +//utility function to insert node at the end of the list |
| 182 | +void insertNode(node* &head,int val) { |
| 183 | + node* newNode = new node(val); |
| 184 | + if(head == NULL) { |
| 185 | + head = newNode; |
| 186 | + return; |
| 187 | + } |
| 188 | + node* temp = head; |
| 189 | + while(temp->next != NULL) temp = temp->next; |
| 190 | + |
| 191 | + temp->next = newNode; |
| 192 | + return; |
| 193 | +} |
| 194 | +//utility function to rotate list by k times |
| 195 | +node* rotateRight(node* head,int k) { |
| 196 | + if(head == NULL||head->next == NULL||k == 0) return head; |
| 197 | + //calculating length |
| 198 | + node* temp = head; |
| 199 | + int length = 1; |
| 200 | + while(temp->next != NULL) { |
| 201 | + ++length; |
| 202 | + temp = temp->next; |
| 203 | + } |
| 204 | + //link last node to first node |
| 205 | + temp->next = head; |
| 206 | + k = k%length; //when k is more than length of list |
| 207 | + int end = length-k; //to get end of the list |
| 208 | + while(end--) temp = temp->next; |
| 209 | + //breaking last node link and pointing to NULL |
| 210 | + head = temp->next; |
| 211 | + temp->next = NULL; |
| 212 | + |
| 213 | + return head; |
| 214 | +} |
| 215 | +
|
| 216 | +//utility function to print list |
| 217 | +void printList(node* head) { |
| 218 | + while(head->next != NULL) { |
| 219 | + cout<<head->num<<"->"; |
| 220 | + head = head->next; |
| 221 | + } |
| 222 | + cout<<head->num<<endl; |
| 223 | + return; |
| 224 | +} |
| 225 | +``` |
| 226 | + |
| 227 | +## Complexity Analysis : |
| 228 | + |
| 229 | +- Time complexity: O(length of list) + O(length of list - (length of list%k)) |
| 230 | + |
| 231 | +- Space complexity: O(1) |
0 commit comments