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 are given a number and we will reverse the first given number of elements, and then for the next set we will reverse the elements. We will see the proper code with implementation.

Examples

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

Explanation − In the given linked list we have to make the group of 3-3 elements which leads to three groups: 1, 2, 3 groups one and we will reverse it as 3, 2, and 1. Similarly, for the second groups 4, 5, and 6 it will be reversed as 6, 5, and 4. In the end, the last group is not of the exact given number size so we will just take the number of elements that are left. So for 7 and 8, we will reverse them as 8 and 7.

Let’s see the different approaches to solving this problem −

Example: Iterative Approach

// 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)

Example: Recursive Approach

In this approach, we are going to use the concept of recursion, let us see the code −

// 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)

Conclusion

In this tutorial, we have implemented a JavaScript program to rotate the given linked list in subgroups of given length. We have implemented two approaches; one is recursive and another one is iterative. Both the approaches works on O(N) time complexity.

Updated on: 12-Apr-2023

120 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements