Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • DSA
  • Interview Problems on Linked List
  • Practice Linked List
  • MCQs on Linked List
  • Linked List Tutorial
  • Types of Linked List
  • Singly Linked List
  • Doubly Linked List
  • Circular Linked List
  • Circular Doubly Linked List
  • Linked List vs Array
  • Time & Space Complexity
  • Advantages & Disadvantages
Open In App
Next Article:
Reverse a Linked List in groups of given size using Stack
Next article icon

Reverse a Linked List in groups of given size using Deque

Last Updated : 12 Sep, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

Given a Singly 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: head: 1 -> 2 -> 3 -> 4 -> 5 -> 6 -> NULL, k = 2 
Output: head: 2 -> 1 -> 4 -> 3 -> 6 -> 5 -> NULL 
Explanation : Linked List is reversed in a group of size k = 2.

Reverse-a-Linked-List-in-groups-of-given-size-1


Input: head: 1 -> 2 -> 3 -> 4 -> 5 -> 6 -> NULL, k = 4 
Output: head:  4 -> 3 -> 2 -> 1 -> 6 -> 5 -> NULL
Explanation : Linked List is reversed in a group of size k = 4.

Reverse-a-Linked-List-in-groups-of-given-size-2

Approach:

The idea is to reverse a linked list in groups of size k using a deque. For each group of size k, store nodes in the deque, then repeatedly swap the data of the first and last nodes by popping from both ends of the deque. Continue this process until all nodes are traversed.

Follow the steps below to solve the problem:

  • Initialize a deque(double-ended queue) of node type.
  • Store the address of the first k nodes in the deque.
  • Pop first and the last value from the deque and swap the data values at those addresses.
  • Repeat step 3 till the deque is not empty.
  • Repeat step 2 for the next k nodes and till we reach to end of the linked list.

Below is the implementation of the above approach:  

C++
// C++ program to reverse a linked list in // groups of given size  #include <bits/stdc++.h> using namespace std;  class Node {   public:     int data;     Node *next;      Node(int x) {         data = x;         next = nullptr;     } };  // Function to reverse the linked list in // groups using deque Node *reverseKGroup(Node *head, int k) {     if (!head || k == 1) {         return head;     }      deque<Node*> dq;      Node *curr = head;      while (curr != nullptr) {         int count = 0;          // Push k nodes into the deque         while (curr != nullptr && count < k) {             dq.push_back(curr);             curr = curr->next;             count++;         }                  // Swap first and last nodes in the deque          while (!dq.empty()) {             Node* front = dq.front();             Node* last = dq.back();             swap(front->data, last->data);              // Pop from the front and back of the deque             if (!dq.empty()) {                 dq.pop_front();             }              if (!dq.empty()) {                 dq.pop_back();             }         }     }          return head; }  void printList(Node *head) {     Node *curr = head;     while (curr != nullptr) {         cout << curr->data << " ";         curr = curr->next;     }     cout << endl; }  int main() {        // Creating a sample singly linked list:     // 1 -> 2 -> 3 -> 4 -> 5 -> 6     Node *head = new Node(1);     head->next = new Node(2);     head->next->next = new Node(3);     head->next->next->next = new Node(4);     head->next->next->next->next = new Node(5);     head->next->next->next->next->next = new Node(6);      head = reverseKGroup(head, 3);          printList(head);      return 0; } 
Java
// Java program to reverse a linked list // in groups of given size  import java.util.Deque; import java.util.LinkedList;  class Node {     int data;     Node next;      Node(int x) {         data = x;         next = null;     } }  class GfG {      // Function to reverse the linked list   	// in groups using deque    	static Node reverseKGroup(Node head, int k) {         if (head == null || k == 1) {             return head;         }          Deque<Node> dq = new LinkedList<>();         Node curr = head;          while (curr != null) {             int count = 0;              // Push k nodes into the deque             while (curr != null && count < k) {                 dq.addLast(curr);                 curr = curr.next;                 count++;             }              // Swap first and last nodes in the deque             while (!dq.isEmpty()) {                 Node front = dq.peekFirst();                 Node last = dq.peekLast();                 swap(front, last);                                 if (!dq.isEmpty()) dq.pollFirst();                 if (!dq.isEmpty()) dq.pollLast();             }         }          return head;     }      static void swap(Node a, Node b) {         int temp = a.data;         a.data = b.data;         b.data = temp;     }      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 singly linked list:         // 1 -> 2 -> 3 -> 4 -> 5 -> 6          Node head = new Node(1);         head.next = new Node(2);         head.next.next = new Node(3);         head.next.next.next = new Node(4);         head.next.next.next.next = new Node(5);         head.next.next.next.next.next = new Node(6);          head = reverseKGroup(head, 3);          printList(head);     } } 
Python
# Python program to reverse a linked list in # groups of given size  from collections import deque  class Node:     def __init__(self, x):         self.data = x         self.next = None  # Function to reverse the linked list in # groups using deque def reverseKGroup(head, k):     if not head or k == 1:         return head      dq = deque()     curr = head      while curr:         count = 0          # Push k nodes into the deque         while curr and count < k:             dq.append(curr)             curr = curr.next             count += 1          # Swap first and last nodes in the deque         while dq:             front = dq[0]             last = dq[-1]             front.data, last.data = last.data, front.data             if dq:                 dq.popleft()             if dq:                 dq.pop()      return head  def printList(head):     curr = head     while curr:         print(curr.data, end=" ")         curr = curr.next     print()  if __name__ == "__main__":        # Creating a sample singly linked list:     # 1 -> 2 -> 3 -> 4 -> 5 -> 6      head = Node(1)     head.next = Node(2)     head.next.next = Node(3)     head.next.next.next = Node(4)     head.next.next.next.next = Node(5)     head.next.next.next.next.next = Node(6)      head = reverseKGroup(head, 3)      printList(head) 
C#
// C# program to reverse a linked list in groups  using System; using System.Collections.Generic;  class Node {     public int data;     public Node next;      public Node(int x) {         data = x;         next = null;     } }  class GfG {      // Function to reverse the linked list in groups      static Node reverseKGroup(Node head, int k) {         if (head == null || k == 1)             return head;          List<Node> q = new List<Node>();         Node curr = head;          while (curr != null) {             int count = 0;              // Store addresses of the k nodes in the list             while (count < k && curr != null) {                 q.Add(curr);                 curr = curr.next;                 count++;             }              // Swap first and last nodes in the list             while (q.Count > 0) {                 Node front = q[0];                 Node last = q[q.Count - 1];                  // Swapping data between the front and last nodes                 int temp = front.data;                 front.data = last.data;                 last.data = temp;                  // Remove from the front and back of the list                 q.RemoveAt(0);                 if (q.Count > 0) {                     q.RemoveAt(q.Count - 1);                 }             }         }          return head;     }      static void printList(Node head) {         Node curr = head;         while (curr != null) {             Console.Write(curr.data + " ");             curr = curr.next;         }         Console.WriteLine();     }      static void Main(string[] args) {                  // Creating a sample singly linked list:         // 1 -> 2 -> 3 -> 4 -> 5 -> 6         Node head = new Node(1);         head.next = new Node(2);         head.next.next = new Node(3);         head.next.next.next = new Node(4);         head.next.next.next.next = new Node(5);         head.next.next.next.next.next = new Node(6);          head = reverseKGroup(head, 3);          printList(head);     } } 
JavaScript
// JavaScript program to reverse a linked  // list in groups of given size  class Node {     constructor(x) {         this.data = x;         this.next = null;     } }  // Function to reverse the linked list // in groups using deque function reverseKGroup(head, k) {     if (!head || k === 1) {         return head;     }      let dq = [];     let curr = head;      while (curr) {         let count = 0;          // Push k nodes into the deque         while (curr && count < k) {             dq.push(curr);             curr = curr.next;             count++;         }          // Swap first and last nodes in the deque         while (dq.length) {             let front = dq[0];             let last = dq[dq.length - 1];             [front.data, last.data] = [last.data, front.data];             dq.shift();              dq.pop();          }     }      return head; }  // Helper function to print the linked list function printList(head) {     let curr = head;     while (curr) {         console.log(curr.data);         curr = curr.next;     }     console.log(); }  // Creating a sample singly linked list: // 1 -> 2 -> 3 -> 4 -> 5 -> 6 let head = new Node(1); head.next = new Node(2); head.next.next = new Node(3); head.next.next.next = new Node(4); head.next.next.next.next = new Node(5); head.next.next.next.next.next = new Node(6);  head = reverseKGroup(head, 3);  printList(head); 

Output
3 2 1 6 5 4  

Time Complexity: O(n), where n is the number of nodes in the linked list.
Auxiliary Space: O(k)

Related articles:

  • Reverse a Linked List in groups of given size using Stack
  • Reverse a singly Linked List in groups of given size (Recursive Approach)
  • Reverse a singly Linked List in groups of given size (Iterative Approach)


Next Article
Reverse a Linked List in groups of given size using Stack

R

Rohit_Dwivedi
Improve
Article Tags :
  • Algorithms
  • DSA
  • Linked List
  • Technical Scripter
  • deque
  • Technical Scripter 2019
Practice Tags :
  • Algorithms
  • Deque
  • Linked List

Similar Reads

  • Reverse a Linked List in groups of given size using Stack
    Given a Singly 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: head: 1 -> 2 -> 3 -> 4 -> 5 -
    8 min read
  • Reverse a Linked List in groups of given size
    Given a Singly 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. Example: Input: head: 1 -> 2 -> 3 -> 4 -> 5 ->
    3 min read
  • Reverse a doubly linked list in groups of given size | Set 2
    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=2Output: 8<->10<->2<->4 Input: List: 1<->2<->3<->4<->5<->6<->7<->8, K=3O
    15+ min read
  • Reverse given Linked List in groups of specific given sizes
    Given the linked list and an array arr[] of size N, the task is to reverse every arr[i] nodes of the list at a time (0 ≤ i < N). Note: If the number of nodes in the list is greater than the sum of array, then the remaining nodes will remain as it is. Examples: Input: head = 1->2->3->4-
    8 min read
  • Reverse a doubly linked list in groups of K size
    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 <-
    15+ min read
  • XOR Linked List - Reverse a Linked List in groups of given size
    Given a XOR linked list and an integer K, the task is to reverse every K nodes in the given XOR linked list. Examples: Input: XLL = 7< – > 6 < – > 8 < – > 11 < – > 3, K = 3 Output: 8 < – > 6 < – > 7 < – > 3 < – > 11 Explanation: Reversing first K(= 3)
    13 min read
  • Reverse a Linked List in groups of given size (Iterative Approach)
    Given a Singly 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: head: 1 -> 2 -> 3 -> 4 -> 5 -
    10 min read
  • Reverse a singly Linked List in groups of given size (Recursive Approach)
    Given a Singly 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. Example: Input: head: 1 -> 2 -> 3 -> 4 -> 5 ->
    9 min read
  • Javascript Program For Reversing A Linked List In Groups Of Given Size - Set 1
    Given a linked list, write a function to reverse every k nodes (where k is an input to the function). Example: Input: 1->2->3->4->5->6->7->8->NULL, K = 3 Output: 3->2->1->6->5->4->8->7->NULL Input: 1->2->3->4->5->6->7->8->NULL,
    3 min read
  • Javascript Program For Reversing A Linked List In Groups Of Given Size- Set 2
    Given a linked list, write a function to reverse every k nodes (where k is an input to the function). Examples: Input: 1->2->3->4->5->6->7->8->NULL and k = 3 Output: 3->2->1->6->5->4->8->7->NULL. Input: 1->2->3->4->5->6->7->8->N
    3 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences