JavaScript Program for Rotate Doubly linked list by N nodes


A doubly linked list is a linear data structure where each node stores the address of the next and previous node. We have given a doubly linked list and we have to rotate the doubly linked list by N nodes and print it. Here N is the positive number and less than or equal to the count of the nodes present in the linked list.

We are not given the specific side to rotate the doubly linked list. So we will rotate the doubly linked list in both ways.

Rotating Doubly Linked List to Counter Clockwise

We have to rotate the nodes of a doubly linked list counter-clockwise by a given number (N) times. And for the node which is present at the edge, they will move all the nodes except the last one in their right rotation by assuming a doubly linked list in a cycle form and then move the last one to the first node or head position. We will implement a proper code to implement the algorithm with an explanation.

Example

Let us assume we have given a doubly linked list as

LL = [1, 2, 3, 4, 5, 6, 7]

The number of rotations of the nodes is 3

Output:

Rotated LL = [5, 6, 7, 1, 2, 3, 4]

Example

In the below example, we rotate a doubly linked list to counter clockwise by 3 modes.

Input: 1 <=> 2 <=> 3 <=> 4 <=> 5 <=> 6 <=> 7 <=> 8 -> null

Expected Output: 6 <=> 7 <=> 8 <=> 1 <=> 2 <=> 3 <=> 4 <=> 5 -> null

// class to create the structure of the nodes
class Node{
   constructor(data) {
      this.value = data;
      this.next = null;
      this.prev = null;
   }
}

// function to print the linked list
function print(head){
   var temp = head;
   var ans = ""
   while(temp.next != null){
      ans += temp.value;
      ans += " <=> "
      temp = temp.next
   }
   ans += temp.value
   ans += " -> null"
   console.log(ans)
}

// function to add data in linked list
function add(data, head, tail){
   var new_node = new Node(data);
   if(head == null){
      head = new_node
      return new_node
   }
   else{
      tail.next = new_node;
      new_node.prev = tail
      return new_node
   }
}

// function to rotate the linked list
function rotate(head, rotations){
   var temp = head;
   
   // getting length of the linked list
   var len = 0;
   while(temp != null){
      len++;
      temp = temp.next;
   }
   temp = head;
   var mid = temp;
   for(var i=0;i<len-rotations;i++){
      mid = mid.next;
   }
   mid.prev.next = null
   head = mid
   head.prev = null
   while(mid.next != null){
      mid = mid.next;
   }
   mid.next = temp;
   return head;
}

// defining linked list
var head = new Node(1)
var tail = head
tail = add(2,head, tail)
tail = add(3,head, tail)
tail = add(4,head, tail)
tail = add(5,head, tail)
tail = add(6,head, tail)
tail = add(7,head, tail)
tail = add(8,head, tail)
// given number
number = 3;
console.log("The given linked list is: ")
print(head)
console.log("The given linked list after 3 rotations is: ")
head = rotate(head,number)
print(head)

Output

The given linked list is: 
1 <=> 2 <=> 3 <=> 4 <=> 5 <=> 6 <=> 7 <=> 8 -> null
The given linked list after 3 rotations is: 
6 <=> 7 <=> 8 <=> 1 <=> 2 <=> 3 <=> 4 <=> 5 -> null

Rotating Doubly Linked List to Clockwise

We have to rotate the nodes of a doubly linked list to clockwise by a given number (N) times. And for the node which is present at the edge, they will move to the first index in their right rotation by assuming a doubly linked list in a cycle form. We will implement a proper code to implement the algorithm with an explanation.

Example:

Let us assume we have given a doubly linked list as

LL = [1, 2, 3, 4, 5, 6, 7]

The number of rotations of the nodes is 3

Output:

Rotated LL = [4,5, 6, 7, 1, 2, 3]

Example

In the below example, we rotate a doubly linked list to clockwise by 3 modes.

Input: 1 <=> 2 <=> 3 <=> 4 <=> 5 <=> 6 <=> 7 <=> 8 -> null

Expected Output: 4 <=> 5 <=> 6 <=> 7 <=> 8 <=> 1 <=> 2 <=> 3 -> null

// class to create the structure of the nodes
class Node{
   constructor(data){
      this.value = data;
      this.next = null;
      this.prev = null;
   }
}
// function to print the linked list
function print(head){
   var temp = head;
   var ans = ""
   while(temp.next != null){
      ans += temp.value;
      ans += " <=> "
      temp = temp.next
   }
   ans += temp.value
   ans += " -> null"
   console.log(ans)
}
// function to add data in linked list
function add(data, head, tail){
   var new_node = new Node(data);
   if(head == null){
      head = new_node
      return new_node
   }
   else{
      tail.next = new_node;
      new_node.prev = tail
      return new_node
   }
}
// function to rotate the linked list
function rotate(head, rotations){
   var temp = head;
   // getting length of the linked list
   var len = 0;
   while(temp != null){
      len++;
      temp = temp.next;
   }
   temp = head;
   var mid = temp;
   for(var i=0;i<rotations;i++){
      mid = mid.next;
   }
   mid.prev.next = null
   head = mid
   head.prev = null
   while(mid.next != null){
      mid = mid.next;
   }
   mid.next = temp;
   return head;
}
// defining linked list
var head = new Node(1)
var tail = head
tail = add(2,head, tail)
tail = add(3,head, tail)
tail = add(4,head, tail)
tail = add(5,head, tail)
tail = add(6,head, tail)
tail = add(7,head, tail)
tail = add(8,head, tail)
// given number
number = 3;
console.log("The given linked list is: ")
print(head)
console.log("The given linked list after 3 rotations is: ")
head = rotate(head, number)
print(head)

Output

The given linked list is: 
1 <=> 2 <=> 3 <=> 4 <=> 5 <=> 6 <=> 7 <=> 8 -> null
The given linked list after 3 rotations is: 
4 <=> 5 <=> 6 <=> 7 <=> 8 <=> 1 <=> 2 <=> 3 -> null

Conclusion

In this tutorial we have implemented a JavaScript program to rotate the doubly linked list by given number of rotations in both clockwise and counter-clockwise direction without using any extra space and in linear time complexity.

Updated on: 13-Apr-2023

137 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements