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 doubly linked list in groups of K size
Next article icon

Reverse a Linked List in groups of given size

Last Updated : 12 Sep, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report
Try it on GfG Practice
redirect icon

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 -> 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

Table of Content

  • [Expected Approach] Using Iterative Method – O(n) Time and O(1) Space
  • [Alternate Approach – 1] Using Recursion – O(n) Time and O(n / k) Space
  • [Alternate Approach – 2] Using Deque – O(n) Time and O(k) Space
  • [Alternate Approach – 3] Using Stack – O(n) Time and O(k) Space

[Expected Approach] Using Iterative Method – O(n) Time and O(1) Space:

To reverse a linked list in groups k nodes, iterate through the list, reversing each group by updating the next pointers. Track the tail of the previous group to link it with the head of the newly reversed group. After reversing each group, update the prev node and move to the start of the next group. Continue this until the entire list is processed. Return the head of the first reversed group as the new head of the list.

Please refer to Reverse a Linked List in groups of given size using Iteration for implementation.

[Alternate Approach – 1] Using Recursion – O(n) Time and O(n / k) Space:

To reverse a linked list in groups of k nodes using recursion, reverse the first k nodes of the list and update the head to point to the new head of this reversed segment. Recursively reverse the remaining portion of the list and connect the tail of the current reversed segment to the head of this recursively reversed portion.

Please refer to Reverse a Linked List in groups of given size using Recursion for implementation.

[Alternate Approach – 2] Using Deque – O(n) Time and O(k) Space:

To reverse a linked list in groups of k nodes using a deque, start traverse the list by adding nodes to the deque in groups of k. For each group, reverse the nodes by repeatedly swapping the data of first and last nodes of the deque.

Please refer to Reverse a Linked List in groups of given size using Deque for implementation.

[Alternate Approach – 3] Using Stack – O(n) Time and O(k) Space:

The idea is to use a stack to store the nodes of the given linked list. Firstly, push the k nodes of the linked list into the stack. Now, pop the nodes one by one and keep track of the previously popped node. Point the next pointer of the prev node to the top element of the stack. Repeat this process, until we reach end of linked list.

Please refer to Reverse a Linked List in groups of given size using Stack for implementation.



Next Article
Reverse a doubly linked list in groups of K size
author
kartik
Improve
Article Tags :
  • DSA
  • Linked List
  • Accolite
  • Adobe
  • Amazon
  • Amazon-Question
  • Hike
  • MakeMyTrip
  • Microsoft
  • Paytm
  • Reverse
  • SAP Labs
  • Snapdeal
  • Snapdeal-Question
  • VMWare
  • Yatra.com-Question
Practice Tags :
  • Accolite
  • Adobe
  • Amazon
  • Hike
  • MakeMyTrip
  • Microsoft
  • Paytm
  • SAP Labs
  • Snapdeal
  • VMWare
  • Linked List
  • Reverse

Similar Reads

  • Reverse a Linked List in groups of given size using Deque
    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 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 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 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
  • 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
  • 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
  • Reverse an Array in groups of given size
    Given an array arr[] and an integer k, the task is to reverse every subarray formed by consecutive K elements. Examples: Input: arr[] = [1, 2, 3, 4, 5, 6, 7, 8, 9], k = 3 Output: 3, 2, 1, 6, 5, 4, 9, 8, 7 Input: arr[] = [1, 2, 3, 4, 5, 6, 7, 8], k = 5 Output: 5, 4, 3, 2, 1, 8, 7, 6 Input: arr[] = [1
    6 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
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