Open In App

Rotate a Linked List

Improve
Improve
Improve
Like Article
Like
Save Article
Save
Share
Report issue
Report

Given a singly linked list, The task is to rotate the linked list counter-clockwise by k nodes.

Examples:

Input: linked list = 10->20->30->40->50->60, k = 4
Output: 50->60->10->20->30->40. 
Explanation: k is smaller than the count of nodes in a linked list so (k+1 )th node i.e. 50 becomes the head node and 60’s next points to 10

Input: linked list = 30->40->50->60, k = 2
Output: 50->60->30->40. 

Recommended Practice

Approach: Below is the idea to solve the problem:

To rotate the linked list, we need to change the next pointer of kth node to NULL, the next pointer of the last node should point to the previous head node, and finally, change the 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 k’s next in a tem pointer and point k’s next to NULL then start traversing from tem and keep traversing till the end and point end node’s next to start node and make tem as the new head.

Follow the below steps to implement the idea:

  • Initialize a count variable with 0 and pointer kthnode pointing to Null and current pointing to head node.
  • Move from current till k-1 and point kthnode to current’s next and current’s next to NULL.
  • Move current from kth node to end node and point current’s next to head.
 

Below image shows how to rotate function works in the code :

Below is the implementation of the above approach:

C++




// C++ program to rotate
// a linked list counter clock wise
  
#include <bits/stdc++.h>
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 of 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 <stdio.h>
#include <stdlib.h>
  
/* 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 of 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 < k and current is not None):
            current = current.next
            count += 1
  
        # If current is None, k is greater than or equal
        # to count of nodes in linked list. Don't change
        # the list in this case
        if current is None:
            return
  
        # current points to kth node. Store it in a variable
        # kth node points to node 40 in the above example
        kthNode = current
  
        # current will point to last node after this loop
        # current will point to node 60 in above example
        while(current.next is not None):
            current = current.next
  
        # Change next of last node to previous head
        # Next of 60 is now changed to node 10
        current.next = self.head
  
        # Change head to (k + 1)th node
        # head is not changed to node 50
        self.head = kthNode.next
  
        # change next of kth node to NULL
        # next of 40 is not NULL
        kthNode.next = None
  
  
# Driver program to test above function
llist = LinkedList()
  
# Create a list 10->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 */


Javascript




<script>
  
// Javascript program to rotate a linked list
  
var head; // head of list
  
    /* Linked list Node */
     class Node {
        constructor(val) {
            this.data = val;
            this.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
    function rotate(k) {
        if (k == 0)
            return;
  
        // Let us understand the 
        // below code for example k = 4
        // and list = 10->20->30->40->50->60.
        var current = head;
  
        // current will either point to kth 
        // or NULL after this
        // loop. current will point to node 
        // 40 in the above example
        var 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
        var 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.
     */
    function push(new_data) {
        /*
         1 & 2: Allocate the Node & Put in the data
         */
   var 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;
    }
  
    function printList() {
   var temp = head;
        while (temp != null) {
            document.write(temp.data + " ");
            temp = temp.next;
        }
        document.write("<br/>");
    }
  
    /* Driver program to test above functions */
      
        // create a list 10->20->30->40->50->60
        for (i = 60; i >= 10; i -= 10)
            push(i);
  
        document.write("Given list<br/>");
        printList();
  
        rotate(4);
  
        document.write("Rotated Linked List<br/>");
        printList();
  
// This code is contributed by todaysgaurav
  
</script>


Output

Given linked list 
10 20 30 40 50 60 
Rotated Linked list 
50 60 10 20 30 40 

Time Complexity: O(N), where N is the number of nodes in Linked List.
Auxiliary Space: O(1)

Another Approach: Rotate the linked list k times by placing the first element at the end.

The idea is to traverse the given list to find the last element and store it in a node. Now we need to make the next of last element as the current head, which we can do by storing head in temporary node. Repeat the process k time.

Follow the steps below to implement the above idea:

  • Return head if the head is NULL or k=0.
  • Initialize a node last and make it point to the last node of the given list.
  • Make a temporary node pointing to head.
  • while k>0 run a loop :
  • make temp as last node and head point to next of head.

Below is the implementation of the above approach:

C++




#include <bits/stdc++.h>
using namespace std;
  
class Node {
public:
    int data;
    Node* next;
};
  
// Function to rotate a linked list.
Node* rotate(Node* head, int k)
{
    // let us consider the example
    // 10->20->30->40->50->60 - k=4
    // initialising 2 nodes temp and last
    Node* last = head;
    Node* temp = head;
  
    // if head is null or k==0 no rotation is required
    if (head == NULL || k == 0) {
        return head;
    }
  
    // Making last point to the last-node of the given
    // linked list in this case 60
    while (last->next != NULL) {
        last = last->next;
    }
  
    // Rotating the linked list k times, one rotation at a
    // time.
    while (k) {
  
        // Make head point to next of head
        // so in the example given above head becomes 20
        head = head->next;
  
        // Making next of temp as NULL
        // In the above example :10->NULL
        temp->next = NULL;
  
        // Making temp as last node
        // (head)20->30->40->50->60->10(last)
        last->next = temp;
        last = temp;
  
        // Point temp to head again for next rotation
        temp = head;
        k--;
    }
  
    return head;
}
  
void printList(Node* n)
{
    while (n != NULL) {
        cout << n->data << " ";
        n = n->next;
    }
    cout << endl;
}
  
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 of the new node
    new_node->next = (*head_ref);
  
    // move the head to point to the new node
    (*head_ref) = new_node;
}
  
int main()
{
    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);
    head = rotate(head, 4);
  
    cout << "\nRotated Linked list \n";
    printList(head);
    return 1;
}
  
// This code is contributed by Rashi Mishr


Java




// Java program to rotate a linked list
  
public class LinkedList {
    Node head;
  
    class Node {
        int data;
        Node next;
        Node(int d)
        {
            data = d;
            next = null;
        }
    }
  
    // Function to rotate a linked list.
    void rotate(int k)
    {
        // let us consider the example
        // 10->20->30->40->50->60 - k=4
        // initialising 2 nodes temp and last
        Node last = head;
        Node temp = head;
          
        // if head is null or k==0 no rotation is required
        if (head == null || k == 0) {
            return;
        }
  
        // Making last point to the last-node of the given
        // linked list in this case 60
        while (last.next != null) {
            last = last.next;
        }
  
        // Rotating the linked list k times, one rotation at a
        // time.
        while (k != 0) {
       
            // Make head point to next of head
            // so in the example given above head becomes 20
            head = head.next;
       
            // Making next of temp as null
            // In the above example :10->null
            temp.next = null;
       
            // Making temp as last node
            // (head)20->30->40->50->60->10(last)
            last.next = temp;
            last = temp;
       
            // Point temp to head again for next rotation
            temp = head;
            k--;
        }
    }
  
    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();
    }
  
    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("\nRotated Linked List");
        llist.printList();
    }
}
// This code is contributed by Yash Agarwal(yashagarwal2852002)


Python3




# Python program for the above approach.
  
# Structure for a linked list node 
class Node:
    def __init__(self,data):
        self.data = data
        self.next = None
          
# Function to rotate a linked list.
def rotate(head,k):
    # let us consider the example
    #  10->20->30->40->50->60 - k=4
    # initialising 2 nodes temp and last
    last = head
    temp = head
      
    # if head is null or k==0 no rotation is required
    if head == None or k == 0:
        return head
          
    # Making last point to the last-node of the given
    # linked list in this case 60
    while last.next != None:
        last = last.next
          
    # Rotating the linked list k times, one rotation at a time.
    while k:
        # Make head point to next of head
        # so in the example given above head becomes 20
        head = head.next
          
        # Making next of temp as NULL
        # In the above example :10->NULL
        temp.next = None
          
        # Making temp as last node
        # (head)20->30->40->50->60->10(last)
        last.next = temp
        last = temp
          
        # Point temp to head again for next rotation
        temp = head
        k -= 1
    return head
      
def printList(head):
    temp = head
    while temp:
        print(temp.data, end = ' ')
        temp = temp.next
    print()
      
def push(head,new_data):
    # allocate node and put data in it
    new_node = Node(new_data)
      
    # link the old list of the new node
    new_node.next = head
      
    # move the head to point to the new node
    head = new_node
    return head
      
head = None
# create a list 10->20->30->40->50->60
for i in range(60,0,-10):
    head = push(head,i)
      
print("Given linked list: ")
printList(head)
head = rotate(head,4)
  
print("Rotated linked list: ")
printList(head)
  
# This code is contributed by hardikkushwaha.


C#




// C# program to rotate a linked list
using System;
  
public class LinkedList {
    Node head;
  
    public class Node {
        public int data;
        public Node next;
        public Node(int d)
        {
            data = d;
            next = null;
        }
    }
  
    // Function to rotate a linked list.
    void rotate(int k)
    {
        // let us consider the example
        // 10->20->30->40->50->60 - k=4
        // initialising 2 nodes temp and last
        Node last = head;
        Node temp = head;
       
        // if head is null or k==0 no rotation is required
        if (head == null || k == 0) {
            return;
        }
       
        // Making last point to the last-node of the given
        // linked list in this case 60
        while (last.next != null) {
            last = last.next;
        }
       
        // Rotating the linked list k times, one rotation at a
        // time.
        while (k != 0) {
            // Make head point to next of head
            // so in the example given above head becomes 20
            head = head.next;
       
            // Making next of temp as NULL
            // In the above example :10->NULL
            temp.next = null;
       
            // Making temp as last node
            // (head)20->30->40->50->60->10(last)
            last.next = temp;
            last = temp;
       
            // Point temp to head again for next rotation
            temp = head;
            k--;
        }
    }
  
    /* 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("\n");
    }
  
    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 Kirti Agarwal


Javascript




// Javascript program to rotate a linked list
var head;
class Node{
    constructor(val){
        this.data = val;
        this.next = null;
    }
}
  
// Function to rotate a linked list.
function rotate(k) 
{
  
    // let us consider the example
    // 10->20->30->40->50->60 - k=4
    // initialising 2 nodes temp and last   
    var last = head;
    var temp = head;
      
    // if head is null or k==0 no rotation is required
    if(head == null || k == 0) return head;
      
    // Making last point to the last-node of the given
    // linked list in this case 60
    while(last.next != null)
        last = last.next;
          
    // Rotating the linked list k times, one rotation at a
    // time.
    while(k != 0){
        // Make head point to next of head
        // so in the example given above head becomes 20
        head = head.next;
          
        // Making next of temp as NULL
        // In the above example :10->NULL
        temp.next = null;
          
        // Making temp as last node
        // (head)20->30->40->50->60->10(last)
        last.next = temp;
        last = temp;
   
        // Point temp to head again for next rotation
        temp = head;
        k--;
    }
}
  
function printList() {
    var temp = head;
    while (temp != null) {
        document.write(temp.data + " ");
        temp = temp.next;
    }
    console.log("\n");
}
  
function push(new_data){
    // 1 & 2: Allocate the Node & Put in the data
    var 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;
}
  
for (let i = 60; i >= 10; i -= 10)
    push(i);
      
console.log("Given list : \n");
printList();
      
rotate(4);
      
console.log("Rotated Linked List : \n");
printList();
  
// This code is contributed by Yash Agarwal(yashagarwal2852002)


Output

Given linked list 
10 20 30 40 50 60 

Rotated Linked list 
50 60 10 20 30 40 

Time Complexity: O(N)
Auxiliary Space: O(1)



Last Updated : 20 Feb, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads