JavaScript Program for Clockwise rotation of Linked List


The basic structure of a Linked List in JavaScript can be created using the class in JavaScript and then the movement of the nodes from one position to another can be done for rotation. In this article, we will learn how to rotate a linked list in a clockwise manner in JavaScript programming language. We will see the code for understating the concepts in deep.

In the given problem we have given a linked list and we have to rotate it in a clockwise manner. This means, we have to put the last element in first place in every move and if given we have to rotate k times, then we have to put the last k element before the head or the starting node of the linked list. To create a linked list as we have seen earlier we need a class to bind the data and the pointer to the next element together.

Structure of Linked List

Example

First, we will create a class node that will store the value of the current node and the pointer to the next node. After that, we will create a push function that will help to create a linked list and at last, we will create a function display that will help to print the linked list. Let’s move to code first −

// creating the class for the linked list
class Node{
   // defining the constructor for class
   constructor(){
      this.next = null; // pointer to hold the next value 
      this.value = 0; // curent value in the linked list 
   }
}
// defining push function for linked list 
function push(head,data){
   var new_node = new Node();
   new_node.value = data;
   if(head == null){
      return new_node;
   }
   var temp = head;
   while(temp.next != null){
      temp = temp.next;
   }
   temp.next = new_node;
   return head;
}
function display(head){
   var temp = head;
   var values = 0;
   while(temp){   
      values = values + temp.value + " -> ";
      temp = temp.next;
   }
   console.log(values + "null")
}
var head = null;
for(var i = 1;i<6;i++){
   head = push(head,i);
}
display(head)

In the above code, we have created a class using the class keyword, and using the ‘this’ keyword we have created a section to store the data and the pointer to the next node in the constructor of the class.

After that, we have defined a push function that will take two parameters first the head of the linked list and second the data of the new node that we want to add to the linked list. In the function, we have created the new node and stored the value in that. We check if the head is empty(which means we are going to add the first element) then we will simply return the new node other wise using the loop we will go to the end of the linked list and add the new node there.

Approach of Problem

After creating the class and defining the required basic functions we will move to the main function where we will define the function to move the last k element to the front of the linked list which indicates the rotation of the linked list. There are two ways by which we can add the last k elements to the first which is equal to the right rotation of the linked list, for example −

We have given a linked list as: 1 -> 2 -> 3 -> 4 -> 5 ->null

We want to rotate the links listed in a clockwise manner a single time then it will look like this −

5 -> 1 -> 2 -> 3 -> 4 -> null

Also for the rotation of the linked list 3 times then the linked list will go like this −

Initially Linked list: 1 -> 2 -> 3 -> 4 -> 5 -> null
After the first rotation: 5 -> 1 -> 2 -> 3 -> 4 -> null
After the second rotation: 4 -> 5 -> 1 -> 2 -> 3 -> null
After the third rotation: 3 -> 4 -> 5 -> 1 -> 2 -> null

We have two approaches to adding the last elements in the front of the linked list, either adding one by one or adding all at once.

Rotating Linked List One by One

Example

In this approach, we will go to the last node then will move it to the before head node and update the head node. Let’s see the code first −

// creating the class for linked list
class Node{
   // defining the constructor for class
   constructor(){
      this.next = null; // pointer to hold the next value 
      this.value = 0; // curent value in the linked list 
   }
}
// defining push function for linked list 
function push(head,data){
   var new_node = new Node();
   new_node.value = data;
   if(head == null){
      return new_node;
   }
   var temp = head;
   while(temp.next != null){
      temp = temp.next;
   }
   temp.next = new_node;
   return head;
}

function display(head){
   var temp = head;
   var values = 0
   while(temp){
      values =  values + temp.value + " -> ";
      temp = temp.next;
   }
   console.log(values + "null")
}
function rotate(head, k){
   while(k--){
      var temp = head;
      while(temp.next.next != null){
         temp = temp.next;
      }
      var new_head = temp.next;
      temp.next = null;
      new_head.next = head;
      head = new_head;
   }
   return head;
}
var head = null;
for(var i = 1;i<6;i++){
   head = push(head,i);
}
head = rotate(head,3);
display(head);

In the above code, we have used the above-defined code for the linked list of basic functions and just added a new function to rotate the linked list.

In the function rotate, we first loop over the linked list k time using the while loop and in each iteration, we have gone to the second last element of the linked list. Then we remove the last element of the linked list from the linked list and put it in front of the linked list that is before the head. In the end, we have returned the new head, and using the display function we have shown the new linked list.

Time and Space Complexity

We have moved over the linked list k time and the size of the linked list is the N so the overall time complexity of the program is the O(N*K). Also, we haven’t used any extra space so the space complexity of the program is O(1), which is constant.

Rotating Linked List at Once

In the previous code, we added the elements one by one and that cost us the time of O(N*N) to make it better we can move over the linked list and get the size of the linked list. After that, we will again move over the linked list and get the last k elements and add them to the front of the linked list which will make the time complexity of the program O(1).

Conclusion

In this tutorial, we have learned how to rotate a linked list in a clockwise manner in JavaScript programming language. We have seen the code for understating the concepts in deep. The basic structure of a Linked List in JavaScript can be created using the class in JavaScript and then the movement of the nodes from one position to another can be done for rotation. The time complexity of the program was O(N*N) which can be further improved to O(N) while the space complexity of the program is O(1).

Updated on: 24-Mar-2023

170 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements