Reverse a doubly linked list in groups of K size
Last Updated :
07 Sep, 2024
Given a Doubly linked list containing n nodes. The task is to reverse every group of k nodes in the list. If the number of nodes is not a multiple of k then left-out nodes, in the end should be considered as a group and must be reversed.
Examples:
Input: 1 <-> 2 <-> 3 <-> 4 <-> 5 <-> 6 <-> NULL, k = 2
Output: 2 <-> 1 <-> 4 <-> 3 <-> 6 <-> 5 <-> NULL.
Explanation : Linked List is reversed in a group of size k = 2.
Input: 1 <-> 2 <-> 3 <-> 4 <-> 5 <-> 6 <-> NULL, k = 4
Output: 4 <-> 3 <-> 2 <-> 1 <-> 6 -> 5 -> NULL.
Explanation : Linked List is reversed in a group of size k = 4.
[Expected Approach - 1] Using Recursion - O(n) Time and O(n) Space:
The idea is to reverse the first k nodes of the list and update the head of the list to the new head of this reversed segment. Then, connect the tail of this reversed segment to the result of recursively reversing the remaining portion of the list.
Follow the steps below to solve the problem:
- If the list is empty, return the head.
- Reverse the first k nodes using the reverseKNodes() function and update the new Head with the reversed list head.
- Connect the tail of the reversed group to the result of recursively reversing the remaining list using the reverseKGroup() function.
- Update next and prev pointers during the reversal.
- At last, return the new Head of the reversed list from the first group.
C++
// C++ code to reverse a doubly linkedÂ
// list in groups of K size
#include <iostream>
using namespace std;
class Node {
public:
int data;
Node *next;
Node *prev;
Node(int x) {
data = x;
next = nullptr;
prev = nullptr;
}
};
// Helper function to reverse K nodes
Node *reverseKNodes(Node *head, int k) {
Node *curr = head, *prev = nullptr, *next = nullptr;
int count = 0;
while (curr != nullptr && count < k) {
next = curr->next;
curr->next = prev;
curr->prev = nullptr;
if (prev != nullptr) {
prev->prev = curr;
}
prev = curr;
curr = next;
count++;
}
return prev;
}
// Recursive function to reverse in groups of K
Node *reverseKGroup(Node *head, int k) {
if (head == nullptr) {
return head;
}
Node *groupHead = nullptr;
Node *newHead = nullptr;
// Move temp to the next group
Node *temp = head;
int count = 0;
while (temp && count < k) {
temp = temp->next;
count++;
}
// Reverse the first K nodes
groupHead = reverseKNodes(head, k);
// Connect the reversed group with the next part
if (newHead == nullptr) {
newHead = groupHead;
}
// Recursion for the next group
head->next = reverseKGroup(temp, k);
if (head->next != nullptr) {
head->next->prev = head;
}
return newHead;
}
void printList(Node *head) {
Node *curr = head;
while (curr != nullptr) {
cout << curr->data << " ";
curr = curr->next;
}
cout << endl;
}
int main() {
// Creating a sample doubly linked list:
// 1 <-> 2 <-> 3 <-> 4 <-> 5 <-> 6
Node *head = new Node(1);
head->next = new Node(2);
head->next->prev = head;
head->next->next = new Node(3);
head->next->next->prev = head->next;
head->next->next->next = new Node(4);
head->next->next->next->prev = head->next->next;
head->next->next->next->next = new Node(5);
head->next->next->next->next->prev = head->next->next->next;
head->next->next->next->next->next = new Node(6);
head->next->next->next->next->next->prev = head->next->next->next->next;
head = reverseKGroup(head, 2);
printList(head);
return 0;
}
C
// C code to reverse a doubly linked
// list in groups of K size
#include <stdio.h>
#include <stdlib.h>
struct Node {
int data;
struct Node* next;
struct Node* prev;
};
// Helper function to reverse K nodes
struct Node* reverseKNodes(struct Node* head, int k) {
struct Node* curr = head;
struct Node* prev = NULL;
struct Node* next = NULL;
int count = 0;
while (curr != NULL && count < k) {
next = curr->next;
curr->next = prev;
curr->prev = NULL;
if (prev != NULL) {
prev->prev = curr;
}
prev = curr;
curr = next;
count++;
}
return prev;
}
// Recursive function to reverse in groups of K
struct Node* reverseKGroup(struct Node* head, int k) {
if (head == NULL) {
return head;
}
struct Node* groupHead = NULL;
struct Node* newHead = NULL;
// Move temp to the next group
struct Node* temp = head;
int count = 0;
while (temp && count < k) {
temp = temp->next;
count++;
}
// Reverse the first K nodes
groupHead = reverseKNodes(head, k);
// Connect the reversed group with the next part
if (newHead == NULL) {
newHead = groupHead;
}
// Recursion for the next group
head->next = reverseKGroup(temp, k);
if (head->next != NULL) {
head->next->prev = head;
}
return newHead;
}
void printList(struct Node* head) {
struct Node* curr = head;
while (curr != NULL) {
printf("%d ", curr->data);
curr = curr->next;
}
printf("\n");
}
struct Node* createNode(int data) {
struct Node* newNode =
(struct Node*)malloc(sizeof(struct Node));
newNode->data = data;
newNode->next = NULL;
newNode->prev = NULL;
return newNode;
}
int main() {
// Creating a sample doubly linked list:
// 1 <-> 2 <-> 3 <-> 4 <-> 5 <-> 6
struct Node* head = createNode(1);
head->next = createNode(2);
head->next->prev = head;
head->next->next = createNode(3);
head->next->next->prev = head->next;
head->next->next->next = createNode(4);
head->next->next->next->prev = head->next->next;
head->next->next->next->next = createNode(5);
head->next->next->next->next->prev = head->next->next->next;
head->next->next->next->next->next = createNode(6);
head->next->next->next->next->next->prev = head->next->next->next->next;
head = reverseKGroup(head, 2);
printList(head);
return 0;
}
Java
// Java code to reverse a doubly linked
// list in groups of K size
class Node {
int data;
Node next;
Node prev;
Node(int x) {
data = x;
next = null;
prev = null;
}
}
// Helper function to reverse K nodes
class GfG {
static Node reverseKNodes(Node head, int k) {
Node curr = head, prev = null, next = null;
int count = 0;
while (curr != null && count < k) {
next = curr.next;
curr.next = prev;
curr.prev = null;
if (prev != null) {
prev.prev = curr;
}
prev = curr;
curr = next;
count++;
}
return prev;
}
// Recursive function to reverse in groups of K
static Node reverseKGroup(Node head, int k) {
if (head == null) {
return head;
}
Node groupHead = null;
Node newHead = null;
// Move temp to the next group
Node temp = head;
int count = 0;
while (temp != null && count < k) {
temp = temp.next;
count++;
}
// Reverse the first K nodes
groupHead = reverseKNodes(head, k);
// Connect the reversed group with the next part
if (newHead == null) {
newHead = groupHead;
}
// Recursion for the next group
head.next = reverseKGroup(temp, k);
if (head.next != null) {
head.next.prev = head;
}
return newHead;
}
// Function to print the doubly linked list
static void printList(Node head) {
Node curr = head;
while (curr != null) {
System.out.print(curr.data + " ");
curr = curr.next;
}
System.out.println();
}
public static void main(String[] args) {
// Creating a sample doubly linked list:
// 1 <-> 2 <-> 3 <-> 4 <-> 5 <-> 6
Node head = new Node(1);
head.next = new Node(2);
head.next.prev = head;
head.next.next = new Node(3);
head.next.next.prev = head.next;
head.next.next.next = new Node(4);
head.next.next.next.prev = head.next.next;
head.next.next.next.next = new Node(5);
head.next.next.next.next.prev = head.next.next.next;
head.next.next.next.next.next = new Node(6);
head.next.next.next.next.next.prev = head.next.next.next.next;
head = reverseKGroup(head, 2);
printList(head);
}
}
Python
# Python code to reverse a doubly linked
# list in groups of K size
class Node:
def __init__(self, data):
self.data = data
self.next = None
self.prev = None
# Helper function to reverse K nodes
def reverseKNodes(head, k):
curr = head
prev = None
next = None
count = 0
while curr is not None and count < k:
next = curr.next
curr.next = prev
curr.prev = None
if prev is not None:
prev.prev = curr
prev = curr
curr = next
count += 1
return prev
# Recursive function to reverse in groups of K
def reverseKGroup(head, k):
if head is None:
return head
groupHead = None
newHead = None
# Move temp to the next group
temp = head
count = 0
while temp and count < k:
temp = temp.next
count += 1
# Reverse the first K nodes
groupHead = reverseKNodes(head, k)
# Connect the reversed group with the next part
if newHead is None:
newHead = groupHead
# Recursion for the next group
head.next = reverseKGroup(temp, k)
if head.next is not None:
head.next.prev = head
return newHead
def printList(head):
curr = head
while curr is not None:
print(curr.data, end=" ")
curr = curr.next
print()
if __name__ == "__main__":
# Creating a sample doubly linked list:
# 1 <-> 2 <-> 3 <-> 4 <-> 5 <-> 6
head = Node(1)
head.next = Node(2)
head.next.prev = head
head.next.next = Node(3)
head.next.next.prev = head.next
head.next.next.next = Node(4)
head.next.next.next.prev = head.next.next
head.next.next.next.next = Node(5)
head.next.next.next.next.prev = head.next.next.next
head.next.next.next.next.next = Node(6)
head.next.next.next.next.next.prev = head.next.next.next.next
head = reverseKGroup(head, 2)
printList(head)
C#
// C# code to reverse a doubly linked
// list in groups of K size
using System;
class Node {
public int data;
public Node next;
public Node prev;
public Node(int x) {
data = x;
next = null;
prev = null;
}
}
// Helper function to reverse K nodes
class GfG {
static Node reverseKNodes(Node head, int k) {
Node curr = head, prev = null, next = null;
int count = 0;
while (curr != null && count < k) {
next = curr.next;
curr.next = prev;
curr.prev = null;
if (prev != null) {
prev.prev = curr;
}
prev = curr;
curr = next;
count++;
}
return prev;
}
// Recursive function to reverse in groups of K
static Node reverseKGroup(Node head, int k) {
if (head == null) {
return head;
}
Node groupHead = null;
Node newHead = null;
// Move temp to the next group
Node temp = head;
int count = 0;
while (temp != null && count < k) {
temp = temp.next;
count++;
}
// Reverse the first K nodes
groupHead = reverseKNodes(head, k);
// Connect the reversed group with the next part
if (newHead == null) {
newHead = groupHead;
}
// Recursion for the next group
head.next = reverseKGroup(temp, k);
if (head.next != null) {
head.next.prev = head;
}
return newHead;
}
static void printList(Node head) {
Node curr = head;
while (curr != null) {
Console.Write(curr.data + " ");
curr = curr.next;
}
Console.WriteLine();
}
static void Main() {
// Creating a sample doubly linked list:
// 1 <-> 2 <-> 3 <-> 4 <-> 5 <-> 6
Node head = new Node(1);
head.next = new Node(2);
head.next.prev = head;
head.next.next = new Node(3);
head.next.next.prev = head.next;
head.next.next.next = new Node(4);
head.next.next.next.prev = head.next.next;
head.next.next.next.next = new Node(5);
head.next.next.next.next.prev = head.next.next.next;
head.next.next.next.next.next = new Node(6);
head.next.next.next.next.next.prev = head.next.next.next.next;
head = reverseKGroup(head, 2);
printList(head);
}
}
JavaScript
// JavaScript code to reverse a doubly linked
// list in groups of K size
class Node {
constructor(data) {
this.data = data;
this.next = null;
this.prev = null;
}
}
// Helper function to reverse K nodes
function reverseKNodes(head, k) {
let curr = head, prev = null, next = null;
let count = 0;
while (curr !== null && count < k) {
next = curr.next;
curr.next = prev;
curr.prev = null;
if (prev !== null) {
prev.prev = curr;
}
prev = curr;
curr = next;
count++;
}
return prev;
}
// Recursive function to reverse in groups of K
function reverseKGroup(head, k) {
if (head === null) {
return head;
}
let groupHead = null;
let newHead = null;
// Move temp to the next group
let temp = head;
let count = 0;
while (temp && count < k) {
temp = temp.next;
count++;
}
// Reverse the first K nodes
groupHead = reverseKNodes(head, k);
// Connect the reversed group with the next part
if (newHead === null) {
newHead = groupHead;
}
// Recursion for the next group
head.next = reverseKGroup(temp, k);
if (head.next !== null) {
head.next.prev = head;
}
return newHead;
}
function printList(head) {
let curr = head;
while (curr !== null) {
console.log(curr.data + " ");
curr = curr.next;
}
console.log();
}
// Creating a sample doubly linked list:
// 1 <-> 2 <-> 3 <-> 4 <-> 5 <-> 6
let head = new Node(1);
head.next = new Node(2);
head.next.prev = head;
head.next.next = new Node(3);
head.next.next.prev = head.next;
head.next.next.next = new Node(4);
head.next.next.next.prev = head.next.next;
head.next.next.next.next = new Node(5);
head.next.next.next.next.prev = head.next.next.next;
head.next.next.next.next.next = new Node(6);
head.next.next.next.next.next.prev = head.next.next.next.next;
head = reverseKGroup(head, 2);
printList(head);
Time complexity: O(n), where n is the number of nodes in linked list.
Auxiliary Space: O(n)
[Expected Approach - 2] Using Iterative Method - O(n) Time and O(1) Space:
The idea is to traverse the list in groups of k nodes, reversing each group. After reversing a group, link it to the previous group by updating the tail pointer. Continue until the entire list is traversed and return the new head.
Follow the steps below to solve the problem:
- Initialize pointers curr to traverse the list, newHead to track the new head of the list, tail to connect the previous group to the current group.
- For each group of k nodes:
- Set groupHead to the current node.
- Then, reverse the group of k nodes by updating next and prev pointers.
- Also, keep track of the prev node (which will be the new head of the reversed group) and the next node (which is the start of the next group).
- Connect the end of the previous group to the start of the current reversed group.
- Repeat the process for the remaining nodes in the list until all nodes are traversed.
Below is the implementation of the above approach:
C++
// C++ code to reverse a doubly linked
// list in groups of K size
#include <iostream>
using namespace std;
class Node {
public:
int data;
Node *next;
Node *prev;
Node(int x) {
data = x;
next = nullptr;
prev = nullptr;
}
};
// Helper function to reverse K nodes iteratively
Node *reverseKGroup(Node *head, int k) {
if (head == nullptr) {
return head;
}
Node *curr = head;
Node *newHead = nullptr;
Node *tail = nullptr;
while (curr != nullptr) {
Node *groupHead = curr;
Node *prev = nullptr;
Node *next = nullptr;
int count = 0;
// Reverse the nodes in the current group
while (curr != nullptr && count < k) {
next = curr->next;
curr->next = prev;
curr->prev = nullptr;
if (prev != nullptr) {
prev->prev = curr;
}
prev = curr;
curr = next;
count++;
}
// If newHead is null, set it to the
// last node of the first group
if (newHead == nullptr) {
newHead = prev;
}
// Connect the previous group to the
// current reversed group
if (tail != nullptr) {
tail->next = prev;
prev->prev = tail;
}
// Move tail to the end of the reversed group
tail = groupHead;
}
return newHead;
}
void printList(Node *head) {
Node *curr = head;
while (curr != nullptr) {
cout << curr->data << " ";
curr = curr->next;
}
cout << endl;
}
int main() {
// Creating a sample doubly linked list:
// 1 <-> 2 <-> 3 <-> 4 <-> 5 <-> 6
Node *head = new Node(1);
head->next = new Node(2);
head->next->prev = head;
head->next->next = new Node(3);
head->next->next->prev = head->next;
head->next->next->next = new Node(4);
head->next->next->next->prev = head->next->next;
head->next->next->next->next = new Node(5);
head->next->next->next->next->prev = head->next->next->next;
head->next->next->next->next->next = new Node(6);
head->next->next->next->next->next->prev = head->next->next->next->next;
head = reverseKGroup(head, 2);
printList(head);
return 0;
}
C
// C code to reverse a doubly linked
// list in groups of K size
#include <stdio.h>
#include <stdlib.h>
struct Node {
int data;
struct Node* next;
struct Node* prev;
};
// Helper function to reverse K nodes iteratively
struct Node* reverseKGroup(struct Node* head, int k) {
if (head == NULL) {
return head;
}
struct Node* curr = head;
struct Node* newHead = NULL;
struct Node* tail = NULL;
while (curr != NULL) {
struct Node* groupHead = curr;
struct Node* prev = NULL;
struct Node* next = NULL;
int count = 0;
// Reverse the nodes in the current group
while (curr != NULL && count < k) {
next = curr->next;
curr->next = prev;
curr->prev = NULL;
if (prev != NULL) {
prev->prev = curr;
}
prev = curr;
curr = next;
count++;
}
// If newHead is null, set it to the
// last node of the first group
if (newHead == NULL) {
newHead = prev;
}
// Connect the previous group
// to the current reversed group
if (tail != NULL) {
tail->next = prev;
prev->prev = tail;
}
// Move tail to the end of
// the reversed group
tail = groupHead;
}
return newHead;
}
void printList(struct Node* head) {
struct Node* curr = head;
while (curr != NULL) {
printf("%d ", curr->data);
curr = curr->next;
}
printf("\n");
}
struct Node* createNode(int data) {
struct Node* newNode =
(struct Node*)malloc(sizeof(struct Node));
newNode->data = data;
newNode->next = NULL;
newNode->prev = NULL;
return newNode;
}
int main() {
// Creating a sample doubly linked list:
// 1 <-> 2 <-> 3 <-> 4 <-> 5 <-> 6
struct Node* head = createNode(1);
head->next = createNode(2);
head->next->prev = head;
head->next->next = createNode(3);
head->next->next->prev = head->next;
head->next->next->next = createNode(4);
head->next->next->next->prev = head->next->next;
head->next->next->next->next = createNode(5);
head->next->next->next->next->prev = head->next->next->next;
head->next->next->next->next->next = createNode(6);
head->next->next->next->next->next->prev = head->next->next->next->next;
head = reverseKGroup(head, 2);
printList(head);
return 0;
}
Java
// Java code to reverse a doubly linked
// list in groups of K size
class Node {
int data;
Node next;
Node prev;
Node(int x) {
data = x;
next = null;
prev = null;
}
}
// Helper function to reverse K nodes iteratively
class GfG {
public static Node reverseKGroup(Node head, int k) {
if (head == null) {
return head;
}
Node curr = head;
Node newHead = null;
Node tail = null;
while (curr != null) {
Node groupHead = curr;
Node prev = null;
Node next = null;
int count = 0;
// Reverse the nodes in the current group
while (curr != null && count < k) {
next = curr.next;
curr.next = prev;
curr.prev = null;
if (prev != null) {
prev.prev = curr;
}
prev = curr;
curr = next;
count++;
}
// If newHead is null, set it to the
// last node of the first group
if (newHead == null) {
newHead = prev;
}
// Connect the previous group to the
// current reversed group
if (tail != null) {
tail.next = prev;
prev.prev = tail;
}
// Move tail to the end of the
//reversed group
tail = groupHead;
}
return newHead;
}
// Function to print the doubly linked list
public static void printList(Node head) {
Node curr = head;
while (curr != null) {
System.out.print(curr.data + " ");
curr = curr.next;
}
System.out.println();
}
public static void main(String[] args) {
// Creating a sample doubly linked list:
// 1 <-> 2 <-> 3 <-> 4 <-> 5 <-> 6
Node head = new Node(1);
head.next = new Node(2);
head.next.prev = head;
head.next.next = new Node(3);
head.next.next.prev = head.next;
head.next.next.next = new Node(4);
head.next.next.next.prev = head.next.next;
head.next.next.next.next = new Node(5);
head.next.next.next.next.prev = head.next.next.next;
head.next.next.next.next.next = new Node(6);
head.next.next.next.next.next.prev = head.next.next.next.next;
head = reverseKGroup(head, 2);
printList(head);
}
}
Python
# Python code to reverse a doubly linked
# list in groups of K size
class Node:
def __init__(self, x):
self.data = x
self.next = None
self.prev = None
# Helper function to reverse K nodes iteratively
def reverseKGroup(head, k):
if head is None:
return head
curr = head
newHead = None
tail = None
while curr is not None:
groupHead = curr
prev = None
next_node = None
count = 0
# Reverse the nodes in the current group
while curr is not None and count < k:
next_node = curr.next
curr.next = prev
curr.prev = None
if prev is not None:
prev.prev = curr
prev = curr
curr = next_node
count += 1
# If newHead is null, set it to the last
# node of the first group
if newHead is None:
newHead = prev
# Connect the previous group to the
# current reversed group
if tail is not None:
tail.next = prev
prev.prev = tail
# Move tail to the end of the reversed group
tail = groupHead
return newHead
def printList(head):
curr = head
while curr is not None:
print(curr.data, end=" ")
curr = curr.next
print()
if __name__ == "__main__":
# Creating a sample doubly linked list:
# 1 <-> 2 <-> 3 <-> 4 <-> 5 <-> 6
head = Node(1)
head.next = Node(2)
head.next.prev = head
head.next.next = Node(3)
head.next.next.prev = head.next
head.next.next.next = Node(4)
head.next.next.next.prev = head.next.next
head.next.next.next.next = Node(5)
head.next.next.next.next.prev = head.next.next.next
head.next.next.next.next.next = Node(6)
head.next.next.next.next.next.prev = head.next.next.next.next
head = reverseKGroup(head, 2)
printList(head)
C#
// C# code to reverse a doubly linked
// list in groups of K size
using System;
class Node {
public int data;
public Node next;
public Node prev;
public Node(int x) {
data = x;
next = null;
prev = null;
}
}
// Helper function to reverse K nodes iteratively
class GFG {
static Node reverseKGroup(Node head, int k) {
if (head == null) {
return head;
}
Node curr = head;
Node newHead = null;
Node tail = null;
while (curr != null) {
Node groupHead = curr;
Node prev = null;
Node next = null;
int count = 0;
// Reverse the nodes in the current group
while (curr != null && count < k) {
next = curr.next;
curr.next = prev;
curr.prev = null;
if (prev != null) {
prev.prev = curr;
}
prev = curr;
curr = next;
count++;
}
// If newHead is null, set it to the
// last node of the first group
if (newHead == null) {
newHead = prev;
}
// Connect the previous group to the
// current reversed group
if (tail != null) {
tail.next = prev;
prev.prev = tail;
}
// Move tail to the end of the reversed group
tail = groupHead;
}
return newHead;
}
static void printList(Node head) {
Node curr = head;
while (curr != null) {
Console.Write(curr.data + " ");
curr = curr.next;
}
Console.WriteLine();
}
static void Main() {
// Creating a sample doubly linked list:
// 1 <-> 2 <-> 3 <-> 4 <-> 5 <-> 6
Node head = new Node(1);
head.next = new Node(2);
head.next.prev = head;
head.next.next = new Node(3);
head.next.next.prev = head.next;
head.next.next.next = new Node(4);
head.next.next.next.prev = head.next.next;
head.next.next.next.next = new Node(5);
head.next.next.next.next.prev = head.next.next.next;
head.next.next.next.next.next = new Node(6);
head.next.next.next.next.next.prev = head.next.next.next.next;
head = reverseKGroup(head, 2);
printList(head);
}
}
JavaScript
// JavaScript code to reverse a doubly linked
// list in groups of K size
class Node {
constructor(x) {
this.data = x;
this.next = null;
this.prev = null;
}
}
// Helper function to reverse K nodes iteratively
function reverseKGroup(head, k) {
if (head === null) {
return head;
}
let curr = head;
let newHead = null;
let tail = null;
while (curr !== null) {
let groupHead = curr;
let prev = null;
let next = null;
let count = 0;
// Reverse the nodes in the current group
while (curr !== null && count < k) {
next = curr.next;
curr.next = prev;
curr.prev = null;
if (prev !== null) {
prev.prev = curr;
}
prev = curr;
curr = next;
count++;
}
// If newHead is null, set it to the last
// node of the first group
if (newHead === null) {
newHead = prev;
}
// Connect the previous group to the
// current reversed group
if (tail !== null) {
tail.next = prev;
prev.prev = tail;
}
// Move tail to the end of the reversed group
tail = groupHead;
}
return newHead;
}
function printList(head) {
let curr = head;
while (curr !== null) {
console.log(curr.data + " ");
curr = curr.next;
}
}
// Creating a sample doubly linked list:
// 1 <-> 2 <-> 3 <-> 4 <-> 5 <-> 6
let head = new Node(1);
head.next = new Node(2);
head.next.prev = head;
head.next.next = new Node(3);
head.next.next.prev = head.next;
head.next.next.next = new Node(4);
head.next.next.next.prev = head.next.next;
head.next.next.next.next = new Node(5);
head.next.next.next.next.prev = head.next.next.next;
head.next.next.next.next.next = new Node(6);
head.next.next.next.next.next.prev = head.next.next.next.next;
head = reverseKGroup(head, 2);
printList(head);
Time complexity: O(n), where n is the number of nodes in linked list.
Auxiliary Space: O(1)
Similar Reads
DSA Tutorial - Learn Data Structures and Algorithms DSA (Data Structures and Algorithms) is the study of organizing data efficiently using data structures like arrays, stacks, and trees, paired with step-by-step procedures (or algorithms) to solve problems effectively. Data structures manage how data is stored and accessed, while algorithms focus on
7 min read
Quick Sort QuickSort is a sorting algorithm based on the Divide and Conquer that picks an element as a pivot and partitions the given array around the picked pivot by placing the pivot in its correct position in the sorted array. It works on the principle of divide and conquer, breaking down the problem into s
12 min read
Merge Sort - Data Structure and Algorithms Tutorials Merge sort is a popular sorting algorithm known for its efficiency and stability. It follows the divide-and-conquer approach. It works by recursively dividing the input array into two halves, recursively sorting the two halves and finally merging them back together to obtain the sorted array. Merge
14 min read
Data Structures Tutorial Data structures are the fundamental building blocks of computer programming. They define how data is organized, stored, and manipulated within a program. Understanding data structures is very important for developing efficient and effective algorithms. What is Data Structure?A data structure is a st
2 min read
Bubble Sort Algorithm Bubble Sort is the simplest sorting algorithm that works by repeatedly swapping the adjacent elements if they are in the wrong order. This algorithm is not suitable for large data sets as its average and worst-case time complexity are quite high.We sort the array using multiple passes. After the fir
8 min read
Breadth First Search or BFS for a Graph Given a undirected graph represented by an adjacency list adj, where each adj[i] represents the list of vertices connected to vertex i. Perform a Breadth First Search (BFS) traversal starting from vertex 0, visiting vertices from left to right according to the adjacency list, and return a list conta
15+ min read
Binary Search Algorithm - Iterative and Recursive Implementation Binary Search Algorithm is a searching algorithm used in a sorted array by repeatedly dividing the search interval in half. The idea of binary search is to use the information that the array is sorted and reduce the time complexity to O(log N). Binary Search AlgorithmConditions to apply Binary Searc
15 min read
Insertion Sort Algorithm Insertion sort is a simple sorting algorithm that works by iteratively inserting each element of an unsorted list into its correct position in a sorted portion of the list. It is like sorting playing cards in your hands. You split the cards into two groups: the sorted cards and the unsorted cards. T
9 min read
Array Data Structure Guide In this article, we introduce array, implementation in different popular languages, its basic operations and commonly seen problems / interview questions. An array stores items (in case of C/C++ and Java Primitive Arrays) or their references (in case of Python, JS, Java Non-Primitive) at contiguous
4 min read
Sorting Algorithms A Sorting Algorithm is used to rearrange a given array or list of elements in an order. For example, a given array [10, 20, 5, 2] becomes [2, 5, 10, 20] after sorting in increasing order and becomes [20, 10, 5, 2] after sorting in decreasing order. There exist different sorting algorithms for differ
3 min read