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) 時間複雜度內工作。
廣告
資料結構
網路
關係資料庫管理系統
作業系統
Java
iOS
HTML
CSS
Android
Python
C 程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP