# 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
var ans = ""
while(temp.next != null){
ans += temp.value;
ans += " -> "
temp = temp.next
}
ans += temp.value
ans += " -> null"
console.log(ans)
}
return tail.next = new Node(data);
}
// function to remove the duplicate numbers
if (!head || number == 1){
}
var temp = new Node();
temp.value = -1;
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;
}
// given number
var number = 3
console.log("The given linked list is: ")
// calling function to reverse elements in group
console.log("The Linked list after reversing elements in alternative groups is: ")


## 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
var ans = ""
while(temp.next != null){
ans += temp.value;
ans += " -> "
temp = temp.next
}
ans += temp.value
ans += " -> null"
console.log(ans)
}
return tail.next = new Node(data);
}
// function to remove the duplicate numbers
return null;
}
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){
return prev;
} else {
prev.next = reverseGroup(cur, number, !move);
}
}
// given number
var number = 3
console.log("The given linked list is: ")
// calling function to reverse elements in group
console.log("The Linked list after reversing elements in alternative groups is: ")


## 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

53 Views