Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
JavaScript Program For Reversing A Linked List In Groups Of Given Size
A linked list is a linear data structure that consists of interconnected nodes. Reversing a linked list means changing the order of all its elements. Reversing a linked list in groups of a given size means we divide the list into groups of k nodes and reverse each group individually.
Problem Statement
Given linked list: 1 -> 2 -> 3 -> 4 -> 5 -> 6 -> 7 -> 8 -> null Given group size: 3 Output: 3 -> 2 -> 1 -> 6 -> 5 -> 4 -> 8 -> 7 -> null
Explanation: We divide the list into groups of 3 nodes each. The first group [1, 2, 3] becomes [3, 2, 1], the second group [4, 5, 6] becomes [6, 5, 4], and the last incomplete group [7, 8] becomes [8, 7].
Approach 1: Iterative Method
This approach uses a dummy node and iteratively reverses each group by adjusting pointers.
// Class to create the structure of nodes
class Node {
constructor(data) {
this.value = data;
this.next = null;
}
}
// Function to print the linked list
function print(head) {
let temp = head;
if (head == null) {
console.log("The given linked list is empty");
return;
}
let ans = "";
while (temp.next != null) {
ans += temp.value + " -> ";
temp = temp.next;
}
ans += temp.value + " -> null";
console.log(ans);
}
// Function to add data in linked list
function add(data, head, tail) {
return tail.next = new Node(data);
}
// Function to reverse linked list in groups
function reverseGroup(head, k) {
if (!head || k == 1) {
return head;
}
let dummy = new Node(-1);
dummy.next = head;
let prev = dummy;
let cur = dummy;
let next = dummy;
// Calculate the length of linked list
let len = 0;
while (cur) {
len++;
cur = cur.next;
}
len--; // Exclude dummy node
// Iterate and reverse groups
while (next) {
cur = prev.next;
next = cur.next;
let toReverse = len > k ? k : len;
for (let i = 1; i
Original linked list:
1 -> 2 -> 3 -> 4 -> 5 -> 6 -> 7 -> 8 -> null
After reversing in groups of 3:
3 -> 2 -> 1 -> 6 -> 5 -> 4 -> 8 -> 7 -> null
Approach 2: Recursive Method
This approach uses recursion to reverse each group and recursively processes the remaining list.
// Class to create the structure of nodes
class Node {
constructor(data) {
this.value = data;
this.next = null;
}
}
// Function to print the linked list
function print(head) {
let temp = head;
if (head == null) {
console.log("The given linked list is empty");
return;
}
let ans = "";
while (temp.next != null) {
ans += temp.value + " -> ";
temp = temp.next;
}
ans += temp.value + " -> null";
console.log(ans);
}
// Function to add data in linked list
function add(data, head, tail) {
return tail.next = new Node(data);
}
// Recursive function to reverse linked list in groups
function reverseGroup(head, k) {
if (head == null) {
return null;
}
let current = head;
let next = null;
let prev = null;
let count = 0;
// Reverse first k nodes
while (count
Original linked list:
1 -> 2 -> 3 -> 4 -> 5 -> 6 -> 7 -> 8 -> null
After reversing in groups of 3:
3 -> 2 -> 1 -> 6 -> 5 -> 4 -> 8 -> 7 -> null
Comparison
| Approach | Time Complexity | Space Complexity | Method |
|---|---|---|---|
| Iterative | O(n) | O(1) | Uses dummy node and pointer manipulation |
| Recursive | O(n) | O(n/k) | Uses recursion call stack |
Conclusion
Both iterative and recursive approaches efficiently reverse a linked list in groups of given size with O(n) time complexity. The iterative method uses constant space, while the recursive method requires additional stack space for function calls.
