Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
JavaScript Program for Rotate Doubly linked list by N nodes
A doubly linked list is a linear data structure where each node stores references to both the next and previous nodes. In this tutorial, we'll learn how to rotate a doubly linked list by N nodes in both clockwise and counter-clockwise directions.
Rotation means moving elements from one end of the list to the other. For N rotations, we take N nodes from one end and move them to the opposite end while maintaining the doubly linked structure.
Understanding Rotation Direction
Counter-clockwise rotation: Move the first N nodes to the end of the list.
Clockwise rotation: Move the last N nodes to the beginning of the list.
Counter-Clockwise Rotation
In counter-clockwise rotation, we move the first N nodes to the end. The algorithm finds the split point at position (length - N), breaks the list, and reconnects the parts.
// Node class for doubly linked list
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 to 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 counter-clockwise
function rotateCounterClockwise(head, rotations) {
var temp = head;
// Get length of the linked list
var len = 0;
while (temp != null) {
len++;
temp = temp.next;
}
// Find the split point
temp = head;
var splitPoint = temp;
for (var i = 0; i 2 3 4 5 6 7 8
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);
console.log("Original linked list:");
print(head);
console.log("After 3 counter-clockwise rotations:");
head = rotateCounterClockwise(head, 3);
print(head);
Original linked list: 1 2 3 4 5 6 7 8 -> null After 3 counter-clockwise rotations: 6 7 8 1 2 3 4 5 -> null
Clockwise Rotation
In clockwise rotation, we move the last N nodes to the beginning. The algorithm finds the split point at position N, breaks the list, and reconnects the parts in reverse order.
// Function to rotate clockwise
function rotateClockwise(head, rotations) {
var temp = head;
// Get length of the linked list
var len = 0;
while (temp != null) {
len++;
temp = temp.next;
}
// Find the split point
temp = head;
var splitPoint = temp;
for (var i = 0; i 2 3 4 5 6 7 8
var head2 = new Node(1);
var tail2 = head2;
tail2 = add(2, head2, tail2);
tail2 = add(3, head2, tail2);
tail2 = add(4, head2, tail2);
tail2 = add(5, head2, tail2);
tail2 = add(6, head2, tail2);
tail2 = add(7, head2, tail2);
tail2 = add(8, head2, tail2);
console.log("Original linked list:");
print(head2);
console.log("After 3 clockwise rotations:");
head2 = rotateClockwise(head2, 3);
print(head2);
Original linked list: 1 2 3 4 5 6 7 8 -> null After 3 clockwise rotations: 4 5 6 7 8 1 2 3 -> null
Algorithm Complexity
| Operation | Time Complexity | Space Complexity |
|---|---|---|
| Find split point | O(n) | O(1) |
| Break and reconnect | O(1) | O(1) |
| Overall | O(n) | O(1) |
Key Points
- Both rotations use the same technique: find split point, break connections, reconnect parts
- Counter-clockwise: split at (length - N), move first part to end
- Clockwise: split at N, move second part to beginning
- Algorithm works in linear time with constant extra space
Conclusion
We've implemented efficient JavaScript functions to rotate doubly linked lists in both directions. The key insight is finding the correct split point and properly reconnecting the prev/next pointers to maintain the doubly linked structure.
