JavaScript 以N个节点为单位旋转双向链表
双向链表是一种线性数据结构,每个节点存储着下一个节点和前一个节点的地址。给定一个双向链表,我们需要以N个节点为单位旋转链表并将其打印出来。这里的N是一个正数,且小于等于链表中节点的数量。
我们没有给定具体旋转的方向,所以我们会同时按照两个方向旋转链表。
逆时针旋转双向链表
我们需要将双向链表的节点逆时针旋转给定的次数(N)。对于位于链表边缘的节点,它们将除了最后一个节点之外的所有节点都向右旋转,假设将双向链表看作一个循环,然后将最后一个节点移动到第一个节点或头部位置。我们将实现一个合适的算法来实现这个操作,并给出解释。
示例
假设我们有一个双向链表:
LL = [1, 2, 3, 4, 5, 6, 7]
节点旋转次数为3
输出:
旋转后的LL = [5, 6, 7, 1, 2, 3, 4]
示例
在下面的示例中,我们将一个双向链表逆时针旋转3次。
输入:1 <=> 2 <=> 3 <=> 4 <=> 5 <=> 6 <=> 7 <=> 8 -> null
期望输出: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)
输出
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
将双向链表顺时针旋转
我们需要将双向链表的节点顺时针旋转给定的次数(N)次。对于位于边缘的节点,它们将在右旋转时移到循环形式的双向链表的第一个索引。我们将实现一段合适的代码,以解释算法。
示例:
假设我们有一个双向链表:
LL = [1, 2, 3, 4, 5, 6, 7]
旋转的次数为3
输出:
旋转后的LL = [4, 5, 6, 7, 1, 2, 3]
示例
在下面的示例中,我们将一个双向链表顺时针旋转3次。
输入:1 <=> 2 <=> 3 <=> 4 <=> 5 <=> 6 <=> 7 <=> 8 -> null
预期输出: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)
输出
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
结论
在本教程中,我们实现了一个JavaScript程序,通过给定的旋转次数,以线性时间复杂度,在不使用额外空间的情况下,对双向链表进行顺时针和逆时针旋转。