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-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) 时间复杂度内工作。
广告