Reverse a Linked List in groups of given size (Iterative Approach)
Given a linked list and an integer K, the task is to reverse every K nodes of the given linked list.
Examples:
Input: 1 -> 2 -> 3 -> 4 -> 5 -> 6 -> 7 -> 8 -> NULL, K = 3
Output: 3 2 1 6 5 4 8 7
Input: 1 -> 2 -> 3 -> 4 -> 5 -> 6 -> 7 -> 8 -> NULL, K = 5
Output: 5 4 3 2 1 8 7 6
Approach: We have already discussed a recursive solution in the posts Set 1 and Set 2. In this post, we will discuss an iterative solution to the above problem. Unlike the above solutions, we do not use any form of the stack to implement our solution. We reverse the first k nodes of the linked list. While reversing, we keep track of the first and last node of the k-reversed linked list using the join and tail pointer. After reversing the k nodes of the linked list, we join the nodes pointed by the tail pointer and join pointer and update them. We repeat this process until all groups of nodes are reversed.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
struct Node {
int data;
struct Node* next;
};
Node* reverse( struct Node* head, int k)
{
Node* prev = NULL;
Node* curr = head;
Node* temp = NULL;
Node* tail = NULL;
Node* newHead = NULL;
Node* join = NULL;
int t = 0;
while (curr) {
t = k;
join = curr;
prev = NULL;
while (curr && t--) {
temp = curr->next;
curr->next = prev;
prev = curr;
curr = temp;
}
if (!newHead)
newHead = prev;
if (tail)
tail->next = prev;
tail = join;
}
return newHead;
}
void push(Node** head_ref, int new_data)
{
Node* new_node = new Node();
new_node->data = new_data;
new_node->next = (*head_ref);
(*head_ref) = new_node;
}
void printList(Node* node)
{
while (node != NULL) {
cout << node->data << " " ;
node = node->next;
}
}
int main()
{
Node* head = NULL;
push(&head, 9);
push(&head, 8);
push(&head, 7);
push(&head, 6);
push(&head, 5);
push(&head, 4);
push(&head, 3);
push(&head, 2);
push(&head, 1);
int k = 3;
cout << "Given linked list \n" ;
printList(head);
head = reverse(head, k);
cout << "\nReversed Linked list \n" ;
printList(head);
return (0);
}
|
Java
class GFG
{
static class Node
{
int data;
Node next;
};
static Node reverse( Node head, int k)
{
Node prev = null ;
Node curr = head;
Node temp = null ;
Node tail = null ;
Node newHead = null ;
Node join = null ;
int t = 0 ;
while (curr != null )
{
t = k;
join = curr;
prev = null ;
while (curr != null && t-- != 0 )
{
temp = curr.next;
curr.next = prev;
prev = curr;
curr = temp;
}
if ((newHead == null ))
newHead = prev;
if (tail != null )
tail.next = prev;
tail = join;
}
return newHead;
}
static Node push(Node head_ref, int new_data)
{
Node new_node = new Node();
new_node.data = new_data;
new_node.next = (head_ref);
(head_ref) = new_node;
return head_ref;
}
static void printList(Node node)
{
while (node != null )
{
System.out.print( node.data + " " );
node = node.next;
}
}
public static void main(String args[])
{
Node head = null ;
head = push(head, 9 );
head = push(head, 8 );
head = push(head, 7 );
head = push(head, 6 );
head = push(head, 5 );
head = push(head, 4 );
head = push(head, 3 );
head = push(head, 2 );
head = push(head, 1 );
int k = 3 ;
System.out.print( "Given linked list \n" );
printList(head);
head = reverse(head, k);
System.out.print( "\nReversed Linked list \n" );
printList(head);
}
}
|
Python3
import math
class Node:
def __init__( self , data):
self .data = data
self . next = None
def reverse(head, k) :
prev = None
curr = head
temp = None
tail = None
newHead = None
join = None
t = 0
while (curr) :
t = k
join = curr
prev = None
while (curr and t > 0 ):
temp = curr. next
curr. next = prev
prev = curr
curr = temp
t = t - 1
if (newHead = = None ):
newHead = prev
if (tail ! = None ):
tail. next = prev
tail = join
return newHead
def push(head_ref, new_data) :
new_node = Node(new_data)
new_node.data = new_data
new_node. next = head_ref
head_ref = new_node
return head_ref
def printList(node):
while (node ! = None ) :
print (node.data, end = " " )
node = node. next
if __name__ = = '__main__' :
head = None
head = push(head, 9 )
head = push(head, 8 )
head = push(head, 7 )
head = push(head, 6 )
head = push(head, 5 )
head = push(head, 4 )
head = push(head, 3 )
head = push(head, 2 )
head = push(head, 1 )
k = 3
print ( "Given linked list " )
printList(head)
head = reverse(head, k)
print ( "\nReversed Linked list " )
printList(head)
|
C#
using System;
class GFG
{
public class Node
{
public int data;
public Node next;
};
static Node reverse( Node head, int k)
{
Node prev = null ;
Node curr = head;
Node temp = null ;
Node tail = null ;
Node newHead = null ;
Node join = null ;
int t = 0;
while (curr != null )
{
t = k;
join = curr;
prev = null ;
while (curr != null && t-- != 0)
{
temp = curr.next;
curr.next = prev;
prev = curr;
curr = temp;
}
if ((newHead == null ))
newHead = prev;
if (tail != null )
tail.next = prev;
tail = join ;
}
return newHead;
}
static Node push(Node head_ref, int new_data)
{
Node new_node = new Node();
new_node.data = new_data;
new_node.next = (head_ref);
(head_ref) = new_node;
return head_ref;
}
static void printList(Node node)
{
while (node != null )
{
Console.Write( node.data + " " );
node = node.next;
}
}
public static void Main(String []args)
{
Node head = null ;
head = push(head, 9);
head = push(head, 8);
head = push(head, 7);
head = push(head, 6);
head = push(head, 5);
head = push(head, 4);
head = push(head, 3);
head = push(head, 2);
head = push(head, 1);
int k = 3;
Console.Write( "Given linked list \n" );
printList(head);
head = reverse(head, k);
Console.Write( "\nReversed Linked list \n" );
printList(head);
}
}
|
Javascript
<script>
class Node
{
constructor()
{
this .data = 0;
this .next = null ;
}
};
function reverse(head, k)
{
var prev = null ;
var curr = head;
var temp = null ;
var tail = null ;
var newHead = null ;
var join = null ;
var t = 0;
while (curr != null )
{
t = k;
join = curr;
prev = null ;
while (curr != null && t-- != 0)
{
temp = curr.next;
curr.next = prev;
prev = curr;
curr = temp;
}
if ((newHead == null ))
newHead = prev;
if (tail != null )
tail.next = prev;
tail = join;
}
return newHead;
}
function push(head_ref, new_data)
{
var new_node = new Node();
new_node.data = new_data;
new_node.next = (head_ref);
(head_ref) = new_node;
return head_ref;
}
function printList(node)
{
while (node != null )
{
document.write( node.data + " " );
node = node.next;
}
}
var head = null ;
head = push(head, 9);
head = push(head, 8);
head = push(head, 7);
head = push(head, 6);
head = push(head, 5);
head = push(head, 4);
head = push(head, 3);
head = push(head, 2);
head = push(head, 1);
var k = 3;
document.write( "Given linked list <br>" );
printList(head);
head = reverse(head, k);
document.write( "<br>Reversed Linked list <br>" );
printList(head);
</script>
|
Output:
Given linked list
1 2 3 4 5 6 7 8 9
Reversed Linked list
3 2 1 6 5 4 9 8 7
Time Complexity: O(n) where n is the number of nodes in the given list.
Space Complexity: O(1)
Last Updated :
03 Nov, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...