Reverse a singly Linked List in groups of given size | Set 4 (Space efficient approach)
Last Updated :
12 Jan, 2022
Given a linked list, write a function to reverse every k node (where k is an input to the function). Examples:
Inputs: 1->2->3->4->5->6->7->8->NULL, k = 3
Output: 3->2->1->6->5->4->8->7->NULL
Inputs: 1->2->3->4->5->6->7->8->NULL, k = 5
Output: 5->4->3->2->1->8->7->6->NULL
Multiple approaches for the above problem have been already discussed in the posts below:
Approach: This article focus on an approach that uses O(1) auxiliary space. Below are the steps to follow:
- Keep track of the first node in a pointer for each group of k elements in the linked list.
- Reverse the order of the next k elements from the first node of the current group using this algorithm.
- After reversing, the first node of the current group will become the last node. Connect it with the k+1th node of the linked list.
- The k+1th node will become the first node of the next group of k elements. Similarly, repeat the process for every group of k elements till the whole linked list has been traversed.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
class Node {
public :
int data;
Node* next;
Node( int d)
{
data = d;
next = NULL;
}
};
void reverse(Node** node, int k)
{
if (k == 1)
return ;
Node* result = NULL;
Node* head = new Node(0);
head->next = *(node);
Node* next = (*node)->next;
int count = 0;
while (next) {
count++;
Node* tmp = next->next;
next->next = *(node);
*(node) = next;
next = tmp;
if (count == k - 1) {
count = 0;
if (result == NULL)
result = *(node);
tmp = head->next;
tmp->next = next;
head->next = *(node);
head = tmp;
*(node) = next;
if (*(node)!= NULL)
next = (*node)->next;
}
}
Node* tmp = head->next;
if (tmp)
tmp->next = NULL;
head->next = *(node);
*(node) = result;
return ;
}
void push(Node** head, int new_data)
{
Node* new_node = new Node(new_data);
new_node->next = *(head);
*(head) = new_node;
}
void printList(Node** head)
{
Node* temp = *(head);
while (temp) {
cout << temp->data << " " ;
temp = temp->next;
}
cout << endl;
}
int main()
{
Node* head = NULL;
push(&head,8);
push(&head,7);
push(&head,6);
push(&head,5);
push(&head,4);
push(&head,3);
push(&head,2);
push(&head,1);
reverse(&head, 3);
printList(&head);
return 0;
}
|
Java
class LinkedList {
Node head;
class Node {
int data;
Node next;
Node( int d)
{
data = d;
next = null ;
}
}
Node reverse(Node node, int k)
{
if (k == 1 )
return node;
Node result = null ;
Node head = new Node( 0 );
head.next = node;
Node next = node.next;
int count = 0 ;
while (next != null ) {
count++;
Node tmp = next.next;
next.next = node;
node = next;
next = tmp;
if (count == k - 1 ) {
count = 0 ;
if (result == null )
result = node;
tmp = head.next;
tmp.next = next;
head.next = node;
head = tmp;
node = next;
if (node != null )
next = node.next;
}
}
Node tmp = head.next;
if (tmp != null )
tmp.next = null ;
head.next = node;
return result;
}
public void push( int new_data)
{
Node new_node = new Node(new_data);
new_node.next = head;
head = new_node;
}
void printList()
{
Node temp = head;
while (temp != null ) {
System.out.print(temp.data + " " );
temp = temp.next;
}
System.out.println();
}
public static void main(String args[])
{
LinkedList llist = new LinkedList();
llist.push( 8 );
llist.push( 7 );
llist.push( 6 );
llist.push( 5 );
llist.push( 4 );
llist.push( 3 );
llist.push( 2 );
llist.push( 1 );
llist.head = llist.reverse(llist.head, 3 );
llist.printList();
}
}
|
Python3
class Node:
def __init__( self , data):
self .data = data
self . next = None
def reverse(head, k):
temp = jump = Node( 0 )
temp. next = left = right = head
while True :
count = 0
while right and count < k:
right = right. next
count + = 1
if count = = k:
pre, cur = right, left
for _ in range (k):
cur. next , cur, pre = pre, cur. next , cur
jump. next , jump, left = pre, left, right
else :
return temp. next
def push(head, new_data):
new_node = Node(new_data)
new_node. next = head
head = new_node
return head
def printList(head):
temp = head
while (temp):
print (temp.data)
temp = temp. next
head = None
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 )
head = reverse(head, 3 )
printList(head)
|
C#
using System;
public class List {
public Node head;
public class Node {
public int data;
public Node next;
public Node( int d) {
data = d;
next = null ;
}
}
Node reverse(Node node, int k) {
if (k == 1)
return node;
Node result = null ;
Node head = new Node(0);
head.next = node;
Node next = node.next;
int count = 0;
while (next != null ) {
count++;
Node tmp1 = next.next;
next.next = node;
node = next;
next = tmp1;
if (count == k - 1) {
count = 0;
if (result == null )
result = node;
tmp1 = head.next;
tmp1.next = next;
head.next = node;
head = tmp1;
node = next;
if (node != null )
next = node.next;
}
}
Node tmp = head.next;
if (tmp != null )
tmp.next = null ;
head.next = node;
return result;
}
public void Push( int new_data) {
Node new_node = new Node(new_data);
new_node.next = head;
head = new_node;
}
void printList() {
Node temp = head;
while (temp != null ) {
Console.Write(temp.data + " " );
temp = temp.next;
}
Console.WriteLine();
}
public static void Main(String []args)
{
List llist = new List();
llist.Push(8);
llist.Push(7);
llist.Push(6);
llist.Push(5);
llist.Push(4);
llist.Push(3);
llist.Push(2);
llist.Push(1);
llist.head = llist.reverse(llist.head, 3);
llist.printList();
}
}
|
Javascript
<script>
var head;
class Node {
constructor(val) {
this .data = val;
this .next = null ;
}
}
function reverse(node , k) {
if (k == 1)
return node;
var result = null ;
var head = new Node(0);
head.next = node;
var next = node.next;
var count = 0;
while (next != null ) {
count++;
var tmp = next.next;
next.next = node;
node = next;
next = tmp;
if (count == k - 1) {
count = 0;
if (result == null )
result = node;
tmp = head.next;
tmp.next = next;
head.next = node;
head = tmp;
node = next;
if (node != null )
next = node.next;
}
}
var tmp = head.next;
if (tmp != null )
tmp.next = null ;
head.next = node;
return result;
}
function push(new_data) {
var new_node = new Node(new_data);
new_node.next = head;
head = new_node;
}
function printList() {
var temp = head;
while (temp != null ) {
document.write(temp.data + " " );
temp = temp.next;
}
document.write();
}
push(8);
push(7);
push(6);
push(5);
push(4);
push(3);
push(2);
push(1);
head = reverse(head, 3);
printList();
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(1)
Share your thoughts in the comments
Please Login to comment...