JavaScript Program For Reversing Alternate K Nodes In A Singly Linked List


Reversing a linked list means arranging all the nodes of the linked list in the opposite manner as they were present earlier or moving the elements present at the last of the linked list towards the head and head nodes towards the tail. Alternate K nodes reversing means reversing the first k elements and then keeping the next k elements the same and again reversing the next k elements and so on. We will see the codes with the different approaches and explanations for each of them.

For example

If the given linked list is the: 
1 -> 2 -> 3 -> 4 -> 5 -> 6 -> 7 -> 8 -> 9 -> 10 -> 11 -> null
If the given value of k is 3 
Output:
3 -> 2 -> 1 -> 4 -> 5 -> 6 -> 9 -> 8 -> 7 -> 10 -> 11 -> null

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;
   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(prev) {
      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;
      // skiping the next elements 
      var num = number;
      while(num && prev != null){
         num--;
         prev = prev.next
      }
   }
   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)
tail = add(9,head, tail)
tail = add(10,head, tail)
tail = add(11,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 alternative groups is: ")
print(head)

Example: Recursive 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;
   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, move){
   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;
      if(move){
         cur.next = prev;
      }
      prev = cur;
      cur = next;
      cnt++;
   }
   if(move){
      head.next = reverseGroup(cur,number, !move)
      return prev;
   } else {
      prev.next = reverseGroup(cur, number, !move);
      return head;
   }
}
// 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)
tail = add(9,head, tail)
tail = add(10,head, tail)
tail = add(11,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,true)
console.log("The Linked list after reversing elements in alternative groups is: ")
print(head)

Conclusion

In this tutorial, we have implemented two approaches in JavaScript programming language for reversing a given linked list in alternative group of k elements. Our codes have the time complexity of O(1) and iterative code works with O(1) space complexity, while recursive with O(N).

Updated on: 12-Apr-2023

69 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements