JavaScript 以给定大小的组来反转链接列表

JavaScript 以给定大小的组来反转链接列表

链表是一个由相互连接的节点组成的线性数据结构。反转链表意味着改变所有元素的顺序。以给定大小的组来反转链表意味着,我们给出一个数字,我们将反转前给定数量的元素,然后对于下一组,我们将反转元素。我们将看到正确的代码和实现。

示例

Given linked list: 1 -> 2 -> 3 -> 4 -> 5 -> 6 -> 7 -> 8 -> null
Given number: 3
Output: 3 -> 2 -> 1 -> 6 -> 5 -> 4 -> 8 -> 7 -> null

解释 - 在给定的链表中,我们需要将每3个元素分为一组,这样可以得到三组:1、2、3组合成的组,我们将其反转为3、2、1。同样地,对于第二组4、5、6,将其反转为6、5、4。最后,最后一组的元素数量不足给定的大小,因此我们只取剩下的元素。所以对于7和8,我们将它们反转为8和7。

让我们来看一下解决这个问题的不同方法−

示例:迭代方法

// 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;
   if(head == null){
      console.log("The given linked list is empty");
   } else {
      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 (next) {
      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;
   }
   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)
// 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 groups of 3 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;
   if(head == null){
      console.log("The given linked list is empty");
   }
   else{
      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 == null){
      return null;
   }
   var cur = head;
   var next = null;
   var prev = null;
   var cnt = 0;
   while (cnt < number &&  cur != null){
      next = cur.next;
      cur.next = prev;
      prev = cur;
      cur = next;
      cnt++;
   }
   if (next != null){
      head.next = reverseGroup(next, number);
   }
   return prev;
}
// 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 
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 groups of 3 elements is: ")
print(head)

结论

在本教程中,我们实现了一个JavaScript程序,用于在给定长度的子组中旋转给定的链表。我们实现了两种方法:递归和迭代。这两种方法的时间复杂度都是O(N)。

Camera课程

Python教程

Java教程

Web教程

数据库教程

图形图像教程

办公软件教程

Linux教程

计算机教程

大数据教程

开发工具教程