Reverse a doubly linked list in groups of given size | Set 2
Last Updated :
23 Jul, 2025
Given a doubly-linked list containing n nodes. The problem is to reverse every group of k nodes in the list.
Examples:
Input: List: 10<->8<->4<->2, K=2
Output: 8<->10<->2<->4
Input: List: 1<->2<->3<->4<->5<->6<->7<->8, K=3
Output: 3<->2<->1<->6<->5<->4<->8<->7
Recursive Approach: The recursive approach to solving this problem is discussed in Set-1 of this article. Here, we are discussing the iterative approach.
Iterative Approach: This approach is a mix of two algorithms - reversing a doubly-linked list and reversing a linked list in a group of a given size. The function reverseK() individually reverses every k size linked list and connects them using prevFirst pointer that keeps track of the node that is bound to come after reversing. Follow the steps below to solve this problem:
- If N is less than equal to 1, then return head.
- Initialize the variables prevFirst as nullptr and curr as head.
- Initialize the variable firstPass as true.
- Traverse over a while loop till curr is not equal to null and perform the following tasks:
- Initialize the variable count as 0.
- Initialize the variables first as curr, next and prev as null.
- Traverse over a while loop till curr is not equal to null and count is less than K and perform the following tasks:
- Set the value of next as curr->next.
- If count equals 0 then set curr->next as null else curr->next as curr->prev.
- Set curr->prev as next, prev as curr and curr as next.
- Increase the value of count by 1.
- If firstPass is true then set head as next->prev and firstPass as false.
- Else, set prevFirst->next as prev.
- Set prevFirst as first.
- After performing the above steps, print the value of head as the answer.
Below is the implementation of the above approach.
C++
// C++ program for the above approach
#include <bits/stdc++.h>
using namespace std;
// A linked list node
class Node {
public:
int data;
Node* next;
Node* prev;
};
// Given a reference (pointer to pointer)
// to the head of a list
// and an int, inserts a new node on the
// front of the list.
void push(Node** head_ref, int new_data)
{
// Allocate node
Node* new_node = new Node();
// Put in the data
new_node->data = new_data;
// Make next of new node as head
// and previous as NULL
new_node->next = (*head_ref);
new_node->prev = NULL;
// Change prev of head node to new node
if ((*head_ref) != NULL)
(*head_ref)->prev = new_node;
// Move the head to point to the new node
(*head_ref) = new_node;
}
// Given a node as prev_node, insert
// a new node after the given node
void insertAfter(Node* prev_node, int new_data)
{
// Check if the given prev_node is NULL
if (prev_node == NULL) {
cout << "the given previous "
<< "node cannot be NULL";
return;
}
// Allocate new node
Node* new_node = new Node();
// Put in the data
new_node->data = new_data;
// Make next of new node as next of prev_node
new_node->next = prev_node->next;
// Make the next of prev_node as new_node
prev_node->next = new_node;
// Make prev_node as previous of new_node
new_node->prev = prev_node;
// Change previous of new_node's next node
if (new_node->next != NULL)
new_node->next->prev = new_node;
}
// Given a reference (pointer to pointer) to the head
// of a DLL and an int, appends a new node at the end
void append(Node** head_ref, int new_data)
{
// Allocate node
Node* new_node = new Node();
Node* last = *head_ref;
// Put in the data
new_node->data = new_data;
// This new node is going to be the last node,
// so make next of it as NULL
new_node->next = NULL;
// If the Linked List is empty,
// then make the new
// node as head
if (*head_ref == NULL) {
new_node->prev = NULL;
*head_ref = new_node;
return;
}
// Else traverse till the last node
while (last->next != NULL)
last = last->next;
// Change the next of last node
last->next = new_node;
// Make last node as previous of new node
new_node->prev = last;
return;
}
// This function prints contents of
// linked list starting from the given node
void printList(Node* node)
{
Node* last;
while (node != NULL) {
cout << " " << node->data << " ";
last = node;
node = node->next;
}
}
Node* reverseK(Node* head, int k)
{
// When head is NULL or linked list
// has a single node we return
if (head == NULL || head->next == NULL)
return head;
// PrevFirst pointer keeps track of
// the node that is to be connected to each
// reversed part.
Node *prevFirst = NULL, *curr = head;
// FirstPass variable is used so that we
// can mark head of the new linkedlist.
bool firstPass = true;
while (curr != NULL) {
int count = 0;
// Next keeps track of the next node of curr
// Prev keeps track of the previous node of curr
Node *first = curr, *next = NULL, *prev = NULL;
while (curr != NULL && count < k) {
// Reversing the doubly linked list by just
// swapping their next and prev pointers
next = curr->next;
if (count == 0)
curr->next = NULL;
else
curr->next = curr->prev;
curr->prev = next;
prev = curr;
curr = next;
count++;
}
if (firstPass) {
// Setting the head of the new linkedlist
head = next->prev;
firstPass = false;
}
else {
prevFirst->next = prev;
}
prevFirst = first;
}
return head;
}
// Driver Code
int main()
{
// Start with the empty list
Node* head = NULL;
// Insert 6. So linked list becomes 6->NULL
append(&head, 6);
// Insert 7 at the beginning. So
// linked list becomes 7->6->NULL
push(&head, 7);
// Insert 1 at the beginning. So
// linked list becomes 1->7->6->NULL
push(&head, 1);
// Insert 4 at the end. So linked
// list becomes 1->7->6->4->NULL
append(&head, 4);
// Insert 8, after 7. So linked
// list becomes 1->7->8->6->4->NULL
insertAfter(head->next, 8);
// list becomes 1->7->8->6->4->9->NULL
append(&head, 9);
head = reverseK(head, 2);
printList(head);
return 0;
}
Java
// Java program for the above approach
import java.util.*;
class GFG{
// A linked list node
static class Node {
int data;
Node next;
Node prev;
};
static Node head = null;
// Given a reference (pointer to pointer)
// to the head of a list
// and an int, inserts a new node on the
// front of the list.
static void push(int new_data)
{
// Allocate node
Node new_node = new Node();
// Put in the data
new_node.data = new_data;
// Make next of new node as head
// and previous as null
new_node.next = head;
new_node.prev = null;
// Change prev of head node to new node
if (head != null)
head.prev = new_node;
// Move the head to point to the new node
head = new_node;
}
// Given a node as prev_node, insert
// a new node after the given node
static void insertAfter(Node prev_node, int new_data)
{
// Check if the given prev_node is null
if (prev_node == null) {
System.out.print("the given previous "
+ "node cannot be null");
return;
}
// Allocate new node
Node new_node = new Node();
// Put in the data
new_node.data = new_data;
// Make next of new node as next of prev_node
new_node.next = prev_node.next;
// Make the next of prev_node as new_node
prev_node.next = new_node;
// Make prev_node as previous of new_node
new_node.prev = prev_node;
// Change previous of new_node's next node
if (new_node.next != null)
new_node.next.prev = new_node;
}
// Given a reference (pointer to pointer) to the head
// of a DLL and an int, appends a new node at the end
static void append(int new_data)
{
// Allocate node
Node new_node = new Node();
Node last = head;
// Put in the data
new_node.data = new_data;
// This new node is going to be the last node,
// so make next of it as null
new_node.next = null;
// If the Linked List is empty,
// then make the new
// node as head
if (head == null) {
new_node.prev = null;
head = new_node;
return;
}
// Else traverse till the last node
while (last.next != null)
last = last.next;
// Change the next of last node
last.next = new_node;
// Make last node as previous of new node
new_node.prev = last;
return;
}
// This function prints contents of
// linked list starting from the given node
static void printList(Node node)
{
Node last = new Node();
while (node != null) {
System.out.print(" " + node.data+ " ");
last = node;
node = node.next;
}
}
static Node reverseK(Node head, int k)
{
// When head is null or linked list
// has a single node we return
if (head == null || head.next == null)
return head;
// PrevFirst pointer keeps track of
// the node that is to be connected to each
// reversed part.
Node prevFirst = null, curr = head;
// FirstPass variable is used so that we
// can mark head of the new linkedlist.
boolean firstPass = true;
while (curr != null) {
int count = 0;
// Next keeps track of the next node of curr
// Prev keeps track of the previous node of curr
Node first = curr, next = null, prev = null;
while (curr != null && count < k) {
// Reversing the doubly linked list by just
// swapping their next and prev pointers
next = curr.next;
if (count == 0)
curr.next = null;
else
curr.next = curr.prev;
curr.prev = next;
prev = curr;
curr = next;
count++;
}
if (firstPass) {
// Setting the head of the new linkedlist
head = next.prev;
firstPass = false;
}
else {
prevFirst.next = prev;
}
prevFirst = first;
}
return head;
}
// Driver Code
public static void main(String[] args)
{
// Start with the empty list
head = null;
// Insert 6. So linked list becomes 6.null
append( 6);
// Insert 7 at the beginning. So
// linked list becomes 7.6.null
push(7);
// Insert 1 at the beginning. So
// linked list becomes 1.7.6.null
push(1);
// Insert 4 at the end. So linked
// list becomes 1.7.6.4.null
append( 4);
// Insert 8, after 7. So linked
// list becomes 1.7.8.6.4.null
insertAfter(head.next, 8);
// list becomes 1.7.8.6.4.9.null
append( 9);
head = reverseK(head, 2);
printList(head);
}
}
// This code contributed by shikhasingrajput
Python3
# Python Code for the above problem.
# Node class
class Node:
# Function to initialise the node object
def __init__(self, data):
self.data = data # Assign data
self.next = None # Initialize next as null
self.prev = None # Initialize previous as null
# Linked List class contains a Node object
class LinkedList:
# Function to initialize head
def __init__(self):
self.head = None
# Function to insert a new node at the beginning
def push(self, new_data):
new_node = Node(new_data)
new_node.next = self.head
new_node.prev = None
if self.head is not None:
self.head.prev = new_node
self.head = new_node
# This function is in LinkedList class. Inserts a
# new node after the given prev_node.
def insertAfter(self, prev_node, new_data):
if prev_node is None:
return
new_node = Node(new_data)
new_node.next = prev_node.next
prev_node.next = new_node
new_node.prev = prev_node
if new_node.next is not None:
new_node.next.prev = new_node
# This function is defined in Linked List class
# Appends a new node at the end.
def append(self, new_data):
new_node = Node(new_data)
last = self.head
if self.head is None:
new_node.prev = None
self.head = new_node
return
while (last.next):
last = last.next
last.next = new_node
new_node.prev = last
# Utility function to print the linked list
def printList(self):
temp = self.head
while(temp):
print(temp.data, end=" ")
temp = temp.next
def reverseK(self, k):
# When head is NULL or linked list
# has a single node we return
if self.head is None or self.head.next is None:
return
# PrevFirst pointer keeps track of
# the node that is to be connected to each
# reversed part.
prevFirst, curr = None, self.head
# FirstPass variable is used so that we
# can mark head of the new linkedlist.
firstPass = True
while curr:
count = 0
# Next keeps track of the next node of curr
# Prev keeps track of the previous node of curr
first, next_node, prev_node = curr, None, None
while curr is not None and count < k:
# Reversing the doubly linked list by just
# swapping their next and prev pointers
next_node = curr.next
if count is 0:
curr.next = None
else:
curr.next = curr.prev
curr.prev = next_node
prev_node = curr
curr = next_node
count += 1
if (firstPass):
# Setting the head of the new linkedlist
self.head = next_node.prev
firstPass = False
else:
prevFirst.next = prev_node
prevFirst = first
return self.head
if __name__ == '__main__':
llist = LinkedList()
# Insert 6. So linked list becomes 6->NULL
llist.append(6)
# Insert 7 at the beginning. So
# linked list becomes 7->6->NULL
llist.push(7)
# Insert 1 at the beginning. So
# linked list becomes 1->7->6->NULL
llist.push(1)
# Insert 4 at the end. So linked
# list becomes 1->7->6->4->NULL
llist.append(4)
# Insert 8, after 7. So linked
# list becomes 1->7->8->6->4->NULL
llist.insertAfter(llist.head.next, 8)
# list becomes 1->7->8->6->4->9->NULL
llist.append(9)
llist.reverseK(2)
llist.printList()
# This code is contributed by lokesh (lokeshmvs21).
C#
// C# program for the above approach
using System;
using System.Collections.Generic;
public class GFG{
// A linked list node
class Node {
public int data;
public Node next;
public Node prev;
};
static Node head = null;
// Given a reference (pointer to pointer)
// to the head of a list
// and an int, inserts a new node on the
// front of the list.
static void push(int new_data)
{
// Allocate node
Node new_node = new Node();
// Put in the data
new_node.data = new_data;
// Make next of new node as head
// and previous as null
new_node.next = head;
new_node.prev = null;
// Change prev of head node to new node
if (head != null)
head.prev = new_node;
// Move the head to point to the new node
head = new_node;
}
// Given a node as prev_node, insert
// a new node after the given node
static void insertAfter(Node prev_node, int new_data)
{
// Check if the given prev_node is null
if (prev_node == null) {
Console.Write("the given previous "
+ "node cannot be null");
return;
}
// Allocate new node
Node new_node = new Node();
// Put in the data
new_node.data = new_data;
// Make next of new node as next of prev_node
new_node.next = prev_node.next;
// Make the next of prev_node as new_node
prev_node.next = new_node;
// Make prev_node as previous of new_node
new_node.prev = prev_node;
// Change previous of new_node's next node
if (new_node.next != null)
new_node.next.prev = new_node;
}
// Given a reference (pointer to pointer) to the head
// of a DLL and an int, appends a new node at the end
static void append(int new_data)
{
// Allocate node
Node new_node = new Node();
Node last = head;
// Put in the data
new_node.data = new_data;
// This new node is going to be the last node,
// so make next of it as null
new_node.next = null;
// If the Linked List is empty,
// then make the new
// node as head
if (head == null) {
new_node.prev = null;
head = new_node;
return;
}
// Else traverse till the last node
while (last.next != null)
last = last.next;
// Change the next of last node
last.next = new_node;
// Make last node as previous of new node
new_node.prev = last;
return;
}
// This function prints contents of
// linked list starting from the given node
static void printList(Node node)
{
Node last = new Node();
while (node != null) {
Console.Write(" " + node.data+ " ");
last = node;
node = node.next;
}
}
static Node reverseK(Node head, int k)
{
// When head is null or linked list
// has a single node we return
if (head == null || head.next == null)
return head;
// PrevFirst pointer keeps track of
// the node that is to be connected to each
// reversed part.
Node prevFirst = null, curr = head;
// FirstPass variable is used so that we
// can mark head of the new linkedlist.
bool firstPass = true;
while (curr != null) {
int count = 0;
// Next keeps track of the next node of curr
// Prev keeps track of the previous node of curr
Node first = curr, next = null, prev = null;
while (curr != null && count < k) {
// Reversing the doubly linked list by just
// swapping their next and prev pointers
next = curr.next;
if (count == 0)
curr.next = null;
else
curr.next = curr.prev;
curr.prev = next;
prev = curr;
curr = next;
count++;
}
if (firstPass) {
// Setting the head of the new linkedlist
head = next.prev;
firstPass = false;
}
else {
prevFirst.next = prev;
}
prevFirst = first;
}
return head;
}
// Driver Code
public static void Main(String[] args)
{
// Start with the empty list
head = null;
// Insert 6. So linked list becomes 6.null
append( 6);
// Insert 7 at the beginning. So
// linked list becomes 7.6.null
push(7);
// Insert 1 at the beginning. So
// linked list becomes 1.7.6.null
push(1);
// Insert 4 at the end. So linked
// list becomes 1.7.6.4.null
append( 4);
// Insert 8, after 7. So linked
// list becomes 1.7.8.6.4.null
insertAfter(head.next, 8);
// list becomes 1.7.8.6.4.9.null
append( 9);
head = reverseK(head, 2);
printList(head);
}
}
// This code is contributed by 29AjayKumar
JavaScript
// JavaScript program for the above approach
class Node{
constructor(data){
this.data = data;
this.next = null;
this.prev = null;
}
}
// Given a reference (pointer to pointer)
// to the head of a list
// and an int, inserts a new node on the
// front of the list.
function push(head_ref, new_data){
// allocate node
let new_node = new Node(new_data);
// make next of new node as head
new_node.next = head_ref;
// change prev of head node to new node
if(head_ref != null) head_ref.prev = new_node;
head_ref = new_node;
return head_ref;
}
// Given a node as prev_node, insert
// a new node after the given node
function insertAfter(prev_node, new_data){
// check if the given prev_node is null
if(prev_node == null){
document.write("the given previous node cannot be NULL");
return;
}
// allocate new node
let new_node = new Node(new_data);
// make next of new node as next of prev_node
new_node.next = prev_node.next;
// make the next of prev_node as new_node
prev_node.next = new_node;
// make prev_node as previous of new_node
new_node.prev = prev_node;
// change previous of new_node's next node
if(new_node.next != null){
new_node.next.prev = new_node;
}
}
// Given a reference (pointer to pointer) to the head
// of a DLL and an int, appends a new node at the end
function append(head_ref, new_data){
// Allocate node
let new_node = new Node(new_data);
let last = head_ref;
// This new node is going to be the last node,
// so make next of it as null
new_node.next = null;
// If the Linked List is empty,
// then make the new
// node as head
if (head_ref == null) {
new_node.prev = null;
head_ref = new_node;
return head_ref;
}
// Else traverse till the last node
while (last.next != null)
last = last.next;
// Change the next of last node
last.next = new_node;
// Make last node as previous of new node
new_node.prev = last;
return head_ref;
}
// This function prints contents of
// linked list starting from the given node
function printList(node){
let last;
while(node != null){
document.write(node.data + " ");
last = node;
node = node.next;
}
}
function reverseK(head, k){
// When head is NULL or linked list
// has a single node we return
if(head == null || head.next == null) return head;
// PrevFirst pointer keeps track of
// the node that is to be connected to each
// reversed part.
let prevFirst = null;
let curr = head;
// FirstPass variable is used so that we
// can mark head of the new linkedlist.
let firstPass = true;
while(curr != null){
let count = 0;
// Next keeps track of the next node of curr
// Prev keeps track of the previous node of curr
let first = curr;
let next = null;
let prev = null;
while(curr != null && count < k){
// Reversing the doubly linked list by just
// swapping their next and prev pointers
next = curr.next;
if(count == 0){
curr.next = null;
}
else{
curr.next = curr.prev;
}
curr.prev = next;
prev = curr;
curr = next;
count++;
}
if(firstPass){
// Setting the head of the new linkedlist
head = next.prev;
firstPass = false;
}else{
prevFirst.next = prev;
}
prevFirst = first;
}
return head;
}
// driver code
// start with the empty list
let head = null;
// Insert 6. So linked list becomes 6->NULL
head = append(head, 6);
// Insert 7 at the beginning. So
// linked list becomes 7->6->NULL
head = push(head, 7);
// Insert 1 at the beginning. So
// linked list becomes 1->7->6->NULL
head = push(head, 1);
// Insert 4 at the end. So linked
// list becomes 1->7->6->4->NULL
head = append(head, 4);
// Insert 8, after 7. So linked
// list becomes 1->7->8->6->4->NULL
insertAfter(head.next, 8);
// list becomes 1->7->8->6->4->9->NULL
head = append(head, 9);
head = reverseK(head, 2);
printList(head);
// This code is contributed by Kirti Agarwal
Time Complexity: O(N)
Auxiliary Space: O(1)
Similar Reads
Basics & Prerequisites
Data Structures
Array Data StructureIn 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
3 min read
String in Data StructureA string is a sequence of characters. The following facts make string an interesting data structure.Small set of elements. Unlike normal array, strings typically have smaller set of items. For example, lowercase English alphabet has only 26 characters. ASCII has only 256 characters.Strings are immut
2 min read
Hashing in Data StructureHashing is a technique used in data structures that efficiently stores and retrieves data in a way that allows for quick access. Hashing involves mapping data to a specific index in a hash table (an array of items) using a hash function. It enables fast retrieval of information based on its key. The
2 min read
Linked List Data StructureA linked list is a fundamental data structure in computer science. It mainly allows efficient insertion and deletion operations compared to arrays. Like arrays, it is also used to implement other data structures like stack, queue and deque. Hereâs the comparison of Linked List vs Arrays Linked List:
2 min read
Stack Data StructureA Stack is a linear data structure that follows a particular order in which the operations are performed. The order may be LIFO(Last In First Out) or FILO(First In Last Out). LIFO implies that the element that is inserted last, comes out first and FILO implies that the element that is inserted first
2 min read
Queue Data StructureA Queue Data Structure is a fundamental concept in computer science used for storing and managing data in a specific order. It follows the principle of "First in, First out" (FIFO), where the first element added to the queue is the first one to be removed. It is used as a buffer in computer systems
2 min read
Tree Data StructureTree Data Structure is a non-linear data structure in which a collection of elements known as nodes are connected to each other via edges such that there exists exactly one path between any two nodes. Types of TreeBinary Tree : Every node has at most two childrenTernary Tree : Every node has at most
4 min read
Graph Data StructureGraph Data Structure is a collection of nodes connected by edges. It's used to represent relationships between different entities. If you are looking for topic-wise list of problems on different topics like DFS, BFS, Topological Sort, Shortest Path, etc., please refer to Graph Algorithms. Basics of
3 min read
Trie Data StructureThe Trie data structure is a tree-like structure used for storing a dynamic set of strings. It allows for efficient retrieval and storage of keys, making it highly effective in handling large datasets. Trie supports operations such as insertion, search, deletion of keys, and prefix searches. In this
15+ min read
Algorithms
Searching AlgorithmsSearching algorithms are essential tools in computer science used to locate specific items within a collection of data. In this tutorial, we are mainly going to focus upon searching in an array. When we search an item in an array, there are two most common algorithms used based on the type of input
2 min read
Sorting AlgorithmsA 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
Introduction to RecursionThe process in which a function calls itself directly or indirectly is called recursion and the corresponding function is called a recursive function. A recursive algorithm takes one step toward solution and then recursively call itself to further move. The algorithm stops once we reach the solution
14 min read
Greedy AlgorithmsGreedy algorithms are a class of algorithms that make locally optimal choices at each step with the hope of finding a global optimum solution. At every step of the algorithm, we make a choice that looks the best at the moment. To make the choice, we sometimes sort the array so that we can always get
3 min read
Graph AlgorithmsGraph is a non-linear data structure like tree data structure. The limitation of tree is, it can only represent hierarchical data. For situations where nodes or vertices are randomly connected with each other other, we use Graph. Example situations where we use graph data structure are, a social net
3 min read
Dynamic Programming or DPDynamic Programming is an algorithmic technique with the following properties.It is mainly an optimization over plain recursion. Wherever we see a recursive solution that has repeated calls for the same inputs, we can optimize it using Dynamic Programming. The idea is to simply store the results of
3 min read
Bitwise AlgorithmsBitwise algorithms in Data Structures and Algorithms (DSA) involve manipulating individual bits of binary representations of numbers to perform operations efficiently. These algorithms utilize bitwise operators like AND, OR, XOR, NOT, Left Shift, and Right Shift.BasicsIntroduction to Bitwise Algorit
4 min read
Advanced
Segment TreeSegment Tree is a data structure that allows efficient querying and updating of intervals or segments of an array. It is particularly useful for problems involving range queries, such as finding the sum, minimum, maximum, or any other operation over a specific range of elements in an array. The tree
3 min read
Pattern SearchingPattern searching algorithms are essential tools in computer science and data processing. These algorithms are designed to efficiently find a particular pattern within a larger set of data. Patten SearchingImportant Pattern Searching Algorithms:Naive String Matching : A Simple Algorithm that works i
2 min read
GeometryGeometry is a branch of mathematics that studies the properties, measurements, and relationships of points, lines, angles, surfaces, and solids. From basic lines and angles to complex structures, it helps us understand the world around us.Geometry for Students and BeginnersThis section covers key br
2 min read
Interview Preparation
Practice Problem