JavaScript 反转链表中的交替 K 个节点

JavaScript 反转链表中的交替 K 个节点

反转链表意味着将链表的所有节点按照与它们之前的顺序相反的方式排列,或者将链表中末尾的元素移到头部,头部的元素移到末尾。交替 K 个节点反转意味着先反转前 k 个元素,然后保持接下来的 k 个元素不变,再反转接下来的 k 个元素,依此类推。我们将看到使用不同方法的代码,并对每种方法进行解释。

例如:

If the given linked list is the: 
1 -> 2 -> 3 -> 4 -> 5 -> 6 -> 7 -> 8 -> 9 -> 10 -> 11 -> null
If the given value of k is 3 
Output:
3 -> 2 -> 1 -> 4 -> 5 -> 6 -> 9 -> 8 -> 7 -> 10 -> 11 -> null

示例:迭代方法

// class to create the structure of the nodes 
class Node{
   constructor(data) {
      this.value = data;
      this.next = 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){
   return tail.next = new Node(data);
}
// function to remove the duplicate numbers 
function reverseGroup(head, number){
   if (!head || number == 1){
      return head;
   }
   var temp = new Node(); 
   temp.value = -1;
   temp.next = head;
   var prev = temp;
   var cur = temp;
   var next = temp;
   // Calculating the length of linked list
   var len = 0;
   while (cur) {
      len++;
      cur = cur.next;
   }
   // Iterating till next is not NULL
   while(prev) {
      cur = prev.next;
      next = cur.next;
      var toL = len > number ? number : len - 1;
      for (var i = 1; i < toL; i++){
         cur.next = next.next;
         next.next = prev.next;
         prev.next = next;
         next = cur.next;
      }
      prev = cur;
      len -= number;
      // skiping the next elements 
      var num = number;
      while(num && prev != null){
         num--;
         prev = prev.next
      }
   }
   return temp.next;
}
// 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)
tail = add(9,head, tail)
tail = add(10,head, tail)
tail = add(11,head, tail)
// given number 
var number = 3
console.log("The given linked list is: ")
print(head)
// calling function to reverse elements in group 
head = reverseGroup(head,number)
console.log("The Linked list after reversing elements in alternative groups is: ")
print(head)

示例:递归方法

// class to create the structure of the nodes 
class Node{
   constructor(data){
      this.value = data;
      this.next = 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){
   return tail.next = new Node(data);
}
// function to remove the duplicate numbers 
function reverseGroup(head, number, move){
   if (head == null) {
      return null;
   }
   var cur = head;
   var next = null;
   var prev = null;
   var cnt = 0;
   while (cnt < number &&  cur != null){
      next = cur.next;
      if(move){
         cur.next = prev;
      }
      prev = cur;
      cur = next;
      cnt++;
   }
   if(move){
      head.next = reverseGroup(cur,number, !move)
      return prev;
   } else {
      prev.next = reverseGroup(cur, number, !move);
      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)
tail = add(9,head, tail)
tail = add(10,head, tail)
tail = add(11,head, tail)
// given number 
var number = 3
console.log("The given linked list is: ")
print(head)
// calling function to reverse elements in group 
head = reverseGroup(head,number,true)
console.log("The Linked list after reversing elements in alternative groups is: ")
print(head)

结论

在本教程中,我们使用JavaScript编程语言实现了两种方法来反转给定的链表,每个k个元素为一组。我们的代码具有O(1)的时间复杂度,迭代代码具有O(1)的空间复杂度,而递归代码具有O(N)的空间复杂度。

Camera课程

Python教程

Java教程

Web教程

数据库教程

图形图像教程

办公软件教程

Linux教程

计算机教程

大数据教程

开发工具教程