Given a singly linked list, rotate the linked list counter-clockwise by k nodes. Where k is a given positive integer. For example, if the given linked list is 10->20->30->40->50->60 and k is 4, the list should be modified to 50->60->10->20->30->40. Assume that k is smaller than the count of nodes in linked list.

Method-1:
To rotate the linked list, we need to change next of kth node to NULL, next of the last node to the previous head node, and finally, change head to (k+1)th node. So we need to get hold of three nodes: kth node, (k+1)th node and last node.
Traverse the list from the beginning and stop at kth node. Store pointer to kth node. We can get (k+1)th node using kthNode->next. Keep traversing till the end and store pointer to last node also. Finally, change pointers as stated above.

Below image shows how rotate function works in the code : ## C++

 `// C++ program to rotate` `// a linked list counter clock wise`   `#include ` `using` `namespace` `std;`   `/* Link list node */` `class` `Node {` `public``:` `    ``int` `data;` `    ``Node* next;` `};`   `// This function rotates a linked list` `// counter-clockwise and updates the` `// head. The function assumes that k is` `// smaller than size of linked list.` `// It doesn't modify the list if` `// k is greater than or equal to size` `void` `rotate(Node** head_ref, ``int` `k)` `{` `    ``if` `(k == 0)` `        ``return``;`   `    ``// Let us understand the below` `    ``// code for example k = 4 and` `    ``// list = 10->20->30->40->50->60.` `    ``Node* current = *head_ref;`   `    ``// current will either point to` `    ``// kth or NULL after this loop.` `    ``// current will point to node` `    ``// 40 in the above example` `    ``int` `count = 1;` `    ``while` `(count < k && current != NULL) {` `        ``current = current->next;` `        ``count++;` `    ``}`   `    ``// If current is NULL, k is greater than` `    ``// or equal to count of nodes in linked list.` `    ``// Don't change the list in this case` `    ``if` `(current == NULL)` `        ``return``;`   `    ``// current points to kth node.` `    ``// Store it in a variable. kthNode` `    ``// points to node 40 in the above example` `    ``Node* kthNode = current;`   `    ``// current will point to` `    ``// last node after this loop` `    ``// current will point to` `    ``// node 60 in the above example` `    ``while` `(current->next != NULL)` `        ``current = current->next;`   `    ``// Change next of last node to previous head` `    ``// Next of 60 is now changed to node 10` `    ``current->next = *head_ref;`   `    ``// Change head to (k+1)th node` `    ``// head is now changed to node 50` `    ``*head_ref = kthNode->next;`   `    ``// change next of kth node to NULL` `    ``// next of 40 is now NULL` `    ``kthNode->next = NULL;` `}`   `/* UTILITY FUNCTIONS */` `/* Function to push a node */` `void` `push(Node** head_ref, ``int` `new_data)` `{` `    ``/* allocate node */` `    ``Node* new_node = ``new` `Node();`   `    ``/* put in the data */` `    ``new_node->data = new_data;`   `    ``/* link the old list off the new node */` `    ``new_node->next = (*head_ref);`   `    ``/* move the head to point to the new node */` `    ``(*head_ref) = new_node;` `}`   `/* Function to print linked list */` `void` `printList(Node* node)` `{` `    ``while` `(node != NULL) {` `        ``cout << node->data << ``" "``;` `        ``node = node->next;` `    ``}` `}`   `/* Driver code*/` `int` `main(``void``)` `{` `    ``/* Start with the empty list */` `    ``Node* head = NULL;`   `    ``// create a list 10->20->30->40->50->60` `    ``for` `(``int` `i = 60; i > 0; i -= 10)` `        ``push(&head, i);`   `    ``cout << ``"Given linked list \n"``;` `    ``printList(head);` `    ``rotate(&head, 4);`   `    ``cout << ``"\nRotated Linked list \n"``;` `    ``printList(head);`   `    ``return` `(0);` `}`   `// This code is contributed by rathbhupendra`

## C

 `// C program to rotate a linked list counter clock wise`   `#include ` `#include `   `/* Link list node */` `struct` `Node {` `    ``int` `data;` `    ``struct` `Node* next;` `};`   `// This function rotates a linked list counter-clockwise and` `// updates the head. The function assumes that k is smaller` `// than size of linked list. It doesn't modify the list if` `// k is greater than or equal to size` `void` `rotate(``struct` `Node** head_ref, ``int` `k)` `{` `    ``if` `(k == 0)` `        ``return``;`   `    ``// Let us understand the below code for example k = 4 and` `    ``// list = 10->20->30->40->50->60.` `    ``struct` `Node* current = *head_ref;`   `    ``// current will either point to kth or NULL after this loop.` `    ``// current will point to node 40 in the above example` `    ``int` `count = 1;` `    ``while` `(count < k && current != NULL) {` `        ``current = current->next;` `        ``count++;` `    ``}`   `    ``// If current is NULL, k is greater than or equal to count` `    ``// of nodes in linked list. Don't change the list in this case` `    ``if` `(current == NULL)` `        ``return``;`   `    ``// current points to kth node. Store it in a variable.` `    ``// kthNode points to node 40 in the above example` `    ``struct` `Node* kthNode = current;`   `    ``// current will point to last node after this loop` `    ``// current will point to node 60 in the above example` `    ``while` `(current->next != NULL)` `        ``current = current->next;`   `    ``// Change next of last node to previous head` `    ``// Next of 60 is now changed to node 10` `    ``current->next = *head_ref;`   `    ``// Change head to (k+1)th node` `    ``// head is now changed to node 50` `    ``*head_ref = kthNode->next;`   `    ``// change next of kth node to NULL` `    ``// next of 40 is now NULL` `    ``kthNode->next = NULL;` `}`   `/* UTILITY FUNCTIONS */` `/* Function to push a node */` `void` `push(``struct` `Node** head_ref, ``int` `new_data)` `{` `    ``/* allocate node */` `    ``struct` `Node* new_node = (``struct` `Node*)``malloc``(``sizeof``(``struct` `Node));`   `    ``/* put in the data  */` `    ``new_node->data = new_data;`   `    ``/* link the old list off the new node */` `    ``new_node->next = (*head_ref);`   `    ``/* move the head to point to the new node */` `    ``(*head_ref) = new_node;` `}`   `/* Function to print linked list */` `void` `printList(``struct` `Node* node)` `{` `    ``while` `(node != NULL) {` `        ``printf``(``"%d "``, node->data);` `        ``node = node->next;` `    ``}` `}`   `/* Driver program to test above function*/` `int` `main(``void``)` `{` `    ``/* Start with the empty list */` `    ``struct` `Node* head = NULL;`   `    ``// create a list 10->20->30->40->50->60` `    ``for` `(``int` `i = 60; i > 0; i -= 10)` `        ``push(&head, i);`   `    ``printf``(``"Given linked list \n"``);` `    ``printList(head);` `    ``rotate(&head, 4);`   `    ``printf``(``"\nRotated Linked list \n"``);` `    ``printList(head);`   `    ``return` `(0);` `}`

## Java

 `// Java program to rotate a linked list`   `class` `LinkedList {` `    ``Node head; ``// head of list`   `    ``/* Linked list Node*/` `    ``class` `Node {` `        ``int` `data;` `        ``Node next;` `        ``Node(``int` `d)` `        ``{` `            ``data = d;` `            ``next = ``null``;` `        ``}` `    ``}`   `    ``// This function rotates a linked list counter-clockwise` `    ``// and updates the head. The function assumes that k is` `    ``// smaller than size of linked list. It doesn't modify` `    ``// the list if k is greater than or equal to size` `    ``void` `rotate(``int` `k)` `    ``{` `        ``if` `(k == ``0``)` `            ``return``;`   `        ``// Let us understand the below code for example k = 4` `        ``// and list = 10->20->30->40->50->60.` `        ``Node current = head;`   `        ``// current will either point to kth or NULL after this` `        ``// loop. current will point to node 40 in the above example` `        ``int` `count = ``1``;` `        ``while` `(count < k && current != ``null``) {` `            ``current = current.next;` `            ``count++;` `        ``}`   `        ``// If current is NULL, k is greater than or equal to count` `        ``// of nodes in linked list. Don't change the list in this case` `        ``if` `(current == ``null``)` `            ``return``;`   `        ``// current points to kth node. Store it in a variable.` `        ``// kthNode points to node 40 in the above example` `        ``Node kthNode = current;`   `        ``// current will point to last node after this loop` `        ``// current will point to node 60 in the above example` `        ``while` `(current.next != ``null``)` `            ``current = current.next;`   `        ``// Change next of last node to previous head` `        ``// Next of 60 is now changed to node 10`   `        ``current.next = head;`   `        ``// Change head to (k+1)th node` `        ``// head is now changed to node 50` `        ``head = kthNode.next;`   `        ``// change next of kth node to null` `        ``kthNode.next = ``null``;` `    ``}`   `    ``/*  Given a reference (pointer to pointer) to the head` `        ``of a list and an int, push a new node on the front` `        ``of the list. */` `    ``void` `push(``int` `new_data)` `    ``{` `        ``/* 1 & 2: Allocate the Node &` `                  ``Put in the data*/` `        ``Node new_node = ``new` `Node(new_data);`   `        ``/* 3. Make next of new Node as head */` `        ``new_node.next = head;`   `        ``/* 4. Move the head to point to new Node */` `        ``head = new_node;` `    ``}`   `    ``void` `printList()` `    ``{` `        ``Node temp = head;` `        ``while` `(temp != ``null``) {` `            ``System.out.print(temp.data + ``" "``);` `            ``temp = temp.next;` `        ``}` `        ``System.out.println();` `    ``}`   `    ``/* Driver program to test above functions */` `    ``public` `static` `void` `main(String args[])` `    ``{` `        ``LinkedList llist = ``new` `LinkedList();`   `        ``// create a list 10->20->30->40->50->60` `        ``for` `(``int` `i = ``60``; i >= ``10``; i -= ``10``)` `            ``llist.push(i);`   `        ``System.out.println(``"Given list"``);` `        ``llist.printList();`   `        ``llist.rotate(``4``);`   `        ``System.out.println(``"Rotated Linked List"``);` `        ``llist.printList();` `    ``}` `} ``/* This code is contributed by Rajat Mishra */`

## Python

 `# Python program to rotate a linked list`   `# Node class ` `class` `Node:`   `    ``# Constructor to initialize the node object` `    ``def` `__init__(``self``, data):` `        ``self``.data ``=` `data` `        ``self``.``next` `=` `None`   `class` `LinkedList:`   `    ``# Function to initialize head` `    ``def` `__init__(``self``):` `        ``self``.head ``=` `None`   `    ``# Function to insert a new node at the beginning` `    ``def` `push(``self``, new_data):` `        ``# allocate node and put the data` `        ``new_node ``=` `Node(new_data)`   `        ``# Make next of new node as head` `        ``new_node.``next` `=` `self``.head` `        `  `        ``# move the head to point to the new Node` `        ``self``.head ``=` `new_node`   `    ``# Utility function to print it the linked LinkedList` `    ``def` `printList(``self``):` `        ``temp ``=` `self``.head` `        ``while``(temp):` `            ``print` `temp.data,` `            ``temp ``=` `temp.``next`   `    ``# This function rotates a linked list counter-clockwise and ` `    ``# updates the head. The function assumes that k is smaller` `    ``# than size of linked list. It doesn't modify the list if` `    ``# k is greater than of equal to size` `    ``def` `rotate(``self``, k):` `        ``if` `k ``=``=` `0``: ` `            ``return` `        `  `        ``# Let us understand the below code for example k = 4` `        ``# and list = 10->20->30->40->50->60` `        ``current ``=` `self``.head` `        `  `        ``# current will either point to kth or NULL after` `        ``# this loop` `        ``# current will point to node 40 in the above example` `        ``count ``=` `1` `        ``while``(count 20->30->40->50->60` `for` `i ``in` `range``(``60``, ``0``, ``-``10``):` `    ``llist.push(i)`   `print` `"Given linked list"` `llist.printList()` `llist.rotate(``4``)`   `print` `"\nRotated Linked list"` `llist.printList()`   `# This code is contributed by Nikhil Kumar Singh(nickzuck_007)`

## C#

 `// C# program to rotate a linked list` `using` `System;`   `public` `class` `LinkedList {` `    ``Node head; ``// head of list`   `    ``/* Linked list Node*/` `    ``public` `class` `Node {` `        ``public` `int` `data;` `        ``public` `Node next;` `        ``public` `Node(``int` `d)` `        ``{` `            ``data = d;` `            ``next = ``null``;` `        ``}` `    ``}`   `    ``// This function rotates a linked list` `    ``// counter-clockwise and updates the head.` `    ``// The function assumes that k is smaller` `    ``// than size of linked list. It doesn't modify` `    ``// the list if k is greater than or equal to size` `    ``void` `rotate(``int` `k)` `    ``{` `        ``if` `(k == 0)` `            ``return``;`   `        ``// Let us understand the below` `        ``// code for example k = 4` `        ``// and list = 10->20->30->40->50->60.` `        ``Node current = head;`   `        ``// current will either point to kth` `        ``// or NULL after this loop. current` `        ``// will point to node 40 in the above example` `        ``int` `count = 1;` `        ``while` `(count < k && current != ``null``) {` `            ``current = current.next;` `            ``count++;` `        ``}`   `        ``// If current is NULL, k is greater than` `        ``// or equal to count of nodes in linked list.` `        ``// Don't change the list in this case` `        ``if` `(current == ``null``)` `            ``return``;`   `        ``// current points to kth node.` `        ``// Store it in a variable.` `        ``// kthNode points to node` `        ``// 40 in the above example` `        ``Node kthNode = current;`   `        ``// current will point to` `        ``// last node after this loop` `        ``// current will point to` `        ``// node 60 in the above example` `        ``while` `(current.next != ``null``)` `            ``current = current.next;`   `        ``// Change next of last node to previous head` `        ``// Next of 60 is now changed to node 10`   `        ``current.next = head;`   `        ``// Change head to (k+1)th node` `        ``// head is now changed to node 50` `        ``head = kthNode.next;`   `        ``// change next of kth node to null` `        ``kthNode.next = ``null``;` `    ``}`   `    ``/* Given a reference (pointer to pointer) to the head` `        ``of a list and an int, push a new node on the front` `        ``of the list. */` `    ``void` `push(``int` `new_data)` `    ``{` `        ``/* 1 & 2: Allocate the Node &` `                ``Put in the data*/` `        ``Node new_node = ``new` `Node(new_data);`   `        ``/* 3. Make next of new Node as head */` `        ``new_node.next = head;`   `        ``/* 4. Move the head to point to new Node */` `        ``head = new_node;` `    ``}`   `    ``void` `printList()` `    ``{` `        ``Node temp = head;` `        ``while` `(temp != ``null``) {` `            ``Console.Write(temp.data + ``" "``);` `            ``temp = temp.next;` `        ``}` `        ``Console.WriteLine();` `    ``}`   `    ``/* Driver code */` `    ``public` `static` `void` `Main()` `    ``{` `        ``LinkedList llist = ``new` `LinkedList();`   `        ``// create a list 10->20->30->40->50->60` `        ``for` `(``int` `i = 60; i >= 10; i -= 10)` `            ``llist.push(i);`   `        ``Console.WriteLine(``"Given list"``);` `        ``llist.printList();`   `        ``llist.rotate(4);`   `        ``Console.WriteLine(``"Rotated Linked List"``);` `        ``llist.printList();` `    ``}` `}`   `/* This code contributed by PrinciRaj1992 */`

Output:

```Given linked list
10  20  30  40  50  60
50  60  10  20  30  40

```

Time Complexity: O(n) where n is the number of nodes in Linked List. The code traverses the linked list only once.

Method-2:
To rotate a linked list by k, we can first make the linked list circular and then moving k-1 steps forward from head node, making it null and make kth node as head.

## C++

 `// C++ program to rotate` `// a linked list counter clock wise`   `#include ` `using` `namespace` `std;`   `/* Link list node */` `class` `Node {` `public``:` `    ``int` `data;` `    ``Node* next;` `};`   `// This function rotates a linked list` `// counter-clockwise and updates the` `// head. The function assumes that k is` `// smaller than size of linked list.` `void` `rotate(Node** head_ref, ``int` `k)` `{` `    ``if` `(k == 0)` `        ``return``;`   `    ``// Let us understand the below` `    ``// code for example k = 4 and` `    ``// list = 10->20->30->40->50->60.` `    ``Node* current = *head_ref;`   `    ``// Traverse till the end.` `    ``while` `(current->next != NULL)` `        ``current = current->next;`   `    ``current->next = *head_ref;` `    ``current = *head_ref;`   `    ``// traverse the linked list to k-1 position which` `    ``// will be last element for rotated array.` `    ``for` `(``int` `i = 0; i < k - 1; i++)` `        ``current = current->next;`   `    ``// update the head_ref and last element pointer to NULL` `    ``*head_ref = current->next;` `    ``current->next = NULL;` `}`   `/* UTILITY FUNCTIONS */` `/* Function to push a node */` `void` `push(Node** head_ref, ``int` `new_data)` `{` `    ``/* allocate node */` `    ``Node* new_node = ``new` `Node();`   `    ``/* put in the data */` `    ``new_node->data = new_data;`   `    ``/* link the old list off the new node */` `    ``new_node->next = (*head_ref);`   `    ``/* move the head to point to the new node */` `    ``(*head_ref) = new_node;` `}`   `/* Function to print linked list */` `void` `printList(Node* node)` `{` `    ``while` `(node != NULL) {` `        ``cout << node->data << ``" "``;` `        ``node = node->next;` `    ``}` `}`   `/* Driver code*/` `int` `main(``void``)` `{` `    ``/* Start with the empty list */` `    ``Node* head = NULL;`   `    ``// create a list 10->20->30->40->50->60` `    ``for` `(``int` `i = 60; i > 0; i -= 10)` `        ``push(&head, i);`   `    ``cout << ``"Given linked list \n"``;` `    ``printList(head);` `    ``rotate(&head, 4);`   `    ``cout << ``"\nRotated Linked list \n"``;` `    ``printList(head);`   `    ``return` `(0);` `}`   `// This code is contributed by pkurada`

Output:

```Given linked list
10 20 30 40 50 60
50 60 10 20 30 40

```

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.

My Personal Notes arrow_drop_up

Article Tags :

36

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.