
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
Reverse a Doubly-Linked List in Groups of a Given Size using C++
In this problem, we are given a pointer to the head of a linked list and an integer k. In groups of size k, we need to reverse the linked list. For example −
Input : 1 <-> 2 <-> 3 <-> 4 <-> 5 (doubly linked list), k = 3 Output : 3 <-> 2 <-> 1 <-> 5 <-> 4
Approach to find The Solution
In this problem, we are going to make a recursive algorithm to solve this problem. In this approach, we are going to use recursion and solve the problem using that.
Example
#include <iostream> using namespace std; struct Node { int data; Node *next, *prev; }; // push function to push a node into the list Node* push(Node* head, int data) { Node* new_node = new Node(); new_node->data = data; new_node->next = NULL; Node* TMP = head; if (head == NULL) { new_node->prev = NULL; head = new_node; return head; } while (TMP->next != NULL) { // going to the last node TMP = TMP->next; } TMP->next = new_node; new_node->prev = TMP; return head; // return pointer to head } // function to print given list void printDLL(Node* head) { while (head != NULL) { cout << head->data << " "; head = head->next; } cout << endl; } Node* revK(Node* head, int k) { if (!head) return NULL; head->prev = NULL; Node *TMP, *CURRENT = head, *newHead; int count = 0; while (CURRENT != NULL && count < k) { // while our count is less than k we simply reverse the nodes. newHead = CURRENT; TMP = CURRENT->prev; CURRENT->prev = CURRENT->next; CURRENT->next = TMP; CURRENT = CURRENT->prev; count++; } if (count >= k) { head->next = revK(CURRENT, k); // now when if the count is greater or equal //to k we connect first head to next head } return newHead; } int main() { Node* head; for (int i = 1; i <= 5; i++) { head = push(head, i); } cout << "Original List : "; printDLL(head); cout << "\nModified List : "; int k = 3; head = revK(head, k); printDLL(head); }
Output
Original List : 1 2 3 4 5 Modified List : 3 2 1 5 4
Explanation of the above code
In this approach, we are traversing through the list and traversing till our count is less than k. We make and recursive call give that value to head -> next( here we are simply reversing the list as we go through, but when our k is reached, we need to make our head point to another lists kth element for example, if our list is 1 2 3 4 5 and our k is 3 in this we reverse the middle elements to 3 2 1 but now we need our 1 to point to 4 as that element is also going to be reversed, so that is why we are using recursive calls and making an extra if statement.).
Conclusion
In this article, we solve a problem to Reverse a doubly-linked list in groups of a given size using recursion. We also learned the C++ program for this problem and the complete approach we solved. We can write the same program in other languages such as C, java, python, and other languages. We hope you find this article helpful.
- Related Articles
- Reverse a Linked List in groups of a Given Size using C++
- Reverse a Doubly Linked List using C++
- JavaScript Program For Reversing A Linked List In Groups Of Given Size
- Program to reverse linked list by groups of size k in Python
- Reverse a Linked List using C++
- Program to find size of Doubly Linked List in C++
- Delete a Doubly Linked List node at a given position in C++
- Creating a Doubly Linked List using Javascript
- Priority Queue using doubly linked list in C++
- Find pairs with given product in a sorted Doubly Linked List in C++
- Delete a node in a Doubly Linked List in C++
- Convert a given Binary Tree to Doubly Linked List (Set 1) in C++
- Convert a given Binary Tree to Doubly Linked List (Set 2) in C++
- Merge Sort for Doubly Linked List using C++.
- Find pairs with given sum in doubly linked list in C++
