# 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
var ans = ""
while(temp.next != null){
ans += temp.value;
ans += " <=> "
temp = temp.next
}
ans += temp.value
ans += " -> null"
console.log(ans)
}

var new_node = new Node(data);
return new_node
}
else{
tail.next = new_node;
new_node.prev = tail
return new_node
}
}

// function to rotate the linked list

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

// given number
number = 3;
console.log("The given linked list is: ")
console.log("The given linked list after 3 rotations is: ")


### 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
var ans = ""
while(temp.next != null){
ans += temp.value;
ans += " <=> "
temp = temp.next
}
ans += temp.value
ans += " -> null"
console.log(ans)
}
var new_node = new Node(data);
return new_node
}
else{
tail.next = new_node;
new_node.prev = tail
return new_node
}
}
// function to rotate the linked list
// getting length of the linked list
var len = 0;
while(temp != null){
len++;
temp = temp.next;
}
var mid = temp;
for(var i=0;i<rotations;i++){
mid = mid.next;
}
mid.prev.next = null
while(mid.next != null){
mid = mid.next;
}
mid.next = temp;
}
// given number
number = 3;
console.log("The given linked list is: ")
console.log("The given linked list after 3 rotations is: ")


### 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

97 Views 