Open In App

Javascript Program For Reversing A Linked List In Groups Of Given Size – Set 1

Last Updated : 16 Dec, 2021
Improve
Improve
Like Article
Like
Save
Share
Report

Given a linked list, write a function to reverse every k nodes (where k is an input to the function). 

Example: 

Input: 1->2->3->4->5->6->7->8->NULL, K = 3 
Output: 3->2->1->6->5->4->8->7->NULL 
Input: 1->2->3->4->5->6->7->8->NULL, K = 5 
Output: 5->4->3->2->1->8->7->6->NULL 

Algorithm: reverse(head, k) 

  • Reverse the first sub-list of size k. While reversing keep track of the next node and previous node. Let the pointer to the next node be next and pointer to the previous node be prev. See this post for reversing a linked list.
  • head->next = reverse(next, k) ( Recursively call for rest of the list and link the two sub-lists )
  • Return prev ( prev becomes the new head of the list (see the diagrams of an iterative method of this post )

Below is image shows how the reverse function works: 

Below is the implementation of the above approach:

Javascript




<script>
// Javascript program to reverse a 
// linked list in groups of
// given size
  
// Head of list
var head; 
  
// Linked list Node 
class Node 
{
    constructor(val) 
    {
        this.data = val;
        this.next = null;
    }
}
  
function reverse(head, k) 
{
    if (head == null)
        return null;
    var current = head;
    var next = null;
    var prev = null;
  
    var count = 0;
  
    // Reverse first k nodes of 
    // linked list 
    while (count < k && 
           current != null
    {
        next = current.next;
        current.next = prev;
        prev = current;
            current = next;
            count++;
    }
  
    /* next is now a pointer to (k+1)th node
       Recursively call for the list starting
       from current. And make rest of the list
       as next of first node. */
    if (next != null)
        head.next = reverse(next, k);
  
    // prev is now head of input list
    return prev;
}
  
// Utility functions 
// Inserts a new Node at front 
// of the list. 
function push(new_data) 
{
    /* 1 & 2: Allocate the Node & 
              Put in the data */
    new_node = new Node(new_data);
  
    // 3. Make next of new Node as head 
    new_node.next = head;
  
    // 4. Move the head to point to
    // new Node 
    head = new_node;
}
  
// Function to print linked list
function printList() 
{
    temp = head;
    while (temp != null
    {
        document.write(temp.data + " ");
        temp = temp.next;
    }
    document.write("<br/>");
}
  
// Driver code  
/* Create Linked List
1->2->3->4->5->6-> 7->8->8->9->null */
push(9);
push(8);
push(7);
push(6);
push(5);
push(4);
push(3);
push(2);
push(1);
  
document.write("Given Linked List<br/>");
printList();
  
head = reverse(head, 3);
  
document.write("Reversed list<br/>");
printList();
// This code is contributed by gauravrajput1
</script>


Output: 

Given Linked List
1 2 3 4 5 6 7 8 9 
Reversed list
3 2 1 6 5 4 9 8 7 

Complexity Analysis: 

  • Time Complexity: O(n). 
    Traversal of list is done only once and it has ‘n’ elements.
  • Auxiliary Space: O(n/k). 
    For each Linked List of size n, n/k or (n/k)+1 calls will be made during the recursion.

Please refer complete article on Reverse a Linked List in groups of given size | Set 1 for more details!



Previous Article
Next Article

Similar Reads

Javascript Program For Reversing A Linked List In Groups Of Given Size- Set 2
Given a linked list, write a function to reverse every k nodes (where k is an input to the function). Examples: Input: 1-&gt;2-&gt;3-&gt;4-&gt;5-&gt;6-&gt;7-&gt;8-&gt;NULL and k = 3 Output: 3-&gt;2-&gt;1-&gt;6-&gt;5-&gt;4-&gt;8-&gt;7-&gt;NULL. Input: 1-&gt;2-&gt;3-&gt;4-&gt;5-&gt;6-&gt;7-&gt;8-&gt;NULL and k = 5 Output: 5-&gt;4-&gt;3-&gt;2-&gt;1-
3 min read
C++ Program For Reversing A Linked List In Groups Of Given Size - Set 1
Given a linked list, write a function to reverse every k nodes (where k is an input to the function). Example: Input: 1-&gt;2-&gt;3-&gt;4-&gt;5-&gt;6-&gt;7-&gt;8-&gt;NULL, K = 3 Output: 3-&gt;2-&gt;1-&gt;6-&gt;5-&gt;4-&gt;8-&gt;7-&gt;NULL Input: 1-&gt;2-&gt;3-&gt;4-&gt;5-&gt;6-&gt;7-&gt;8-&gt;NULL, K = 5 Output: 5-&gt;4-&gt;3-&gt;2-&gt;1-&gt;8-&gt;
3 min read
C Program For Reversing A Linked List In Groups Of Given Size - Set 1
Given a linked list, write a function to reverse every k nodes (where k is an input to the function). Example: Input: 1-&gt;2-&gt;3-&gt;4-&gt;5-&gt;6-&gt;7-&gt;8-&gt;NULL, K = 3 Output: 3-&gt;2-&gt;1-&gt;6-&gt;5-&gt;4-&gt;8-&gt;7-&gt;NULL Input: 1-&gt;2-&gt;3-&gt;4-&gt;5-&gt;6-&gt;7-&gt;8-&gt;NULL, K = 5 Output: 5-&gt;4-&gt;3-&gt;2-&gt;1-&gt;8-&gt;
3 min read
Java Program For Reversing A Linked List In Groups Of Given Size - Set 1
Given a linked list, write a function to reverse every k nodes (where k is an input to the function).  Example:  Input: 1->2->3->4->5->6->7->8->NULL, K = 3 Output: 3->2->1->6->5->4->8->7->NULL Input: 1->2->3->4->5->6->7->8->NULL, K = 5 Output: 5->4->3->2->1->8->7->6->NULL  Recommended: Please solve it on "PRACTICE" first, before moving on to the so
3 min read
Python Program For Reversing A Linked List In Groups Of Given Size - Set 1
Given a linked list, write a function to reverse every k nodes (where k is an input to the function).  Example:  Input: 1->2->3->4->5->6->7->8->NULL, K = 3 Output: 3->2->1->6->5->4->8->7->NULL Input: 1->2->3->4->5->6->7->8->NULL, K = 5 Output: 5->4->3->2->1->8->7->6->NULL  Recommended: Please solve it on "PRACTICE" first, before moving on to the so
3 min read
C++ Program For Reversing A Linked List In Groups Of Given Size - Set 2
Given a linked list, write a function to reverse every k nodes (where k is an input to the function). Examples: Input: 1-&gt;2-&gt;3-&gt;4-&gt;5-&gt;6-&gt;7-&gt;8-&gt;NULL and k = 3 Output: 3-&gt;2-&gt;1-&gt;6-&gt;5-&gt;4-&gt;8-&gt;7-&gt;NULL. Input: 1-&gt;2-&gt;3-&gt;4-&gt;5-&gt;6-&gt;7-&gt;8-&gt;NULL and k = 5 Output: 5-&gt;4-&gt;3-&gt;2-&gt;1-
3 min read
Java Program For Reversing A Linked List In Groups Of Given Size- Set 2
Given a linked list, write a function to reverse every k nodes (where k is an input to the function). Examples: Input: 1-&gt;2-&gt;3-&gt;4-&gt;5-&gt;6-&gt;7-&gt;8-&gt;NULL and k = 3 Output: 3-&gt;2-&gt;1-&gt;6-&gt;5-&gt;4-&gt;8-&gt;7-&gt;NULL. Input: 1-&gt;2-&gt;3-&gt;4-&gt;5-&gt;6-&gt;7-&gt;8-&gt;NULL and k = 5 Output: 5-&gt;4-&gt;3-&gt;2-&gt;1-
3 min read
Python Program For Reversing A Linked List In Groups Of Given Size- Set 2
Given a linked list, write a function to reverse every k nodes (where k is an input to the function). Examples: Input: 1->2->3->4->5->6->7->8->NULL and k = 3 Output: 3->2->1->6->5->4->8->7->NULL. Input: 1->2->3->4->5->6->7->8->NULL and k = 5 Output: 5->4->3->2->1->8->7->6->NULL. Recommended: Please solve it on "PRACTICE" first, before moving on to
3 min read
XOR Linked List - Reverse a Linked List in groups of given size
Given a XOR linked list and an integer K, the task is to reverse every K nodes in the given XOR linked list. Examples: Input: XLL = 7&lt; – &gt; 6 &lt; – &gt; 8 &lt; – &gt; 11 &lt; – &gt; 3, K = 3 Output: 8 &lt; – &gt; 6 &lt; – &gt; 7 &lt; – &gt; 3 &lt; – &gt; 11 Explanation: Reversing first K(= 3) nodes modifies the Linked List to 8 &lt; – &gt; 6
13 min read
Reverse a singly Linked List in groups of given size | Set 3
Given a singly linked list and an integer K, the task is to reverse every K nodes of the given linked list. Examples: Input: 1 -&gt; 2 -&gt; 3 -&gt; 4 -&gt; 5 -&gt; 6 -&gt; 7 -&gt; 8 -&gt; NULL, K = 3 Output: 3 2 1 6 5 4 8 7 Input: 1 -&gt; 2 -&gt; 3 -&gt; 4 -&gt; 5 -&gt; 6 -&gt; 7 -&gt; 8 -&gt; NULL, K = 5 Output: 5 4 3 2 1 8 7 6 Approach: Two diff
11 min read