C Program to Reverse a linked list in groups of given size

Reverse a linked list in groups of given size

C Program to Reverse a linked list in groups of given size. In this we will reverse the sub-nodes of the linked list according to the user given size, it is somehow same as reverse the nodes of the linked list which is not a big deal to reverse nodes of the linked list.
For Example:-
Input:- 31 -> 12 -> 4-> 24 -> 65 -> 76 ->7 -> 18; m = 2(given size by user)
Output:- 12 -> 31 -> 24 -> 4 -> 76 -> 65 -> 18 -> 7; reverse of nodes as per user group size is 2.
To learn more about this steps and code are given in C Programming Language.

C Program to Reverse a linked list in groups of given size

Steps required for Reverse a linked list in groups of given size:-

  • First of all we have to reverse the first sub nodes of the size m according to the user requirement.
  • While reversing the linked list keep observe the next node and previous node.
  • Now lets take the pointer to the next node be next and pointer to the previous node be prev.
  • Then take as head -> next = reverse (next, m).
  • Return prev.
C Program to reverse a linked list in groups of given size

Structure for creating node in the singly linked list:-

struct node
{
int data;
struct node *next;
};

C Program to Reverse a linked list in groups of given size

Run
#include<stdio.h>
#include<stdlib.h>
struct Node			// Structure of node
{
  int item;
  struct Node *next;
};

/* this function reverses the linked list in groups of size m and 
  returns the pointer to the new head node*/

struct Node *reverse (struct Node *head, int m)
{
  struct Node *current = head;
  struct Node *next = NULL;
  struct Node *previous = NULL;
  int counter = 0;
  //reverse first m nodes of the linked list
  while (current != NULL && counter < m) { next = current->next;
      current->next = previous;
      previous = current;
      current = next;
      counter++;
    }

  if (next != NULL)
    head->next = reverse (next, m);
  return previous;
}

void print_List (struct Node *node)	//print function of the list
{
  while (node != NULL)
    {
      printf ("%d ", node->item);
      node = node->next;
    }
}

void insert (struct Node **head_ref, int new_data)	//insert the data
{
  struct Node *new_node = (struct Node *) malloc (sizeof (struct Node));
  new_node->item = new_data;
  new_node->next = (*head_ref);
  (*head_ref) = new_node;
}


int main ()
{
  struct Node *head = NULL;
  insert (&head, 91);
  insert (&head, 58);
  insert (&head, 17);
  insert (&head, 45);
  insert (&head, 53);
  insert (&head, 1);
  insert (&head, 8);
  insert (&head, 64);
  insert (&head, 15);
  insert (&head, 20);
  printf ("The linked list is: \n");
  print_List (head);
  head = reverse (head, 4);
  printf
    ("\nReverse a Linked List in groups of given size like m = 4 here: \n");
  print_List (head);
  return (0);
}
Output:-
The linked list is: 
20  15  64  8  1  53  45  17  58  91
Reverse a Linked List in groups of given size like m = 4 here: 
8   64  15  20  17  45  53  1  91  58

Prime Course Trailer

Related Banners

Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription

Get over 200+ course One Subscription

Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others

Checkout list of all the video courses in PrepInsta Prime Subscription

Checkout list of all the video courses in PrepInsta Prime Subscription

Singly Linked List

  • Introduction to Linked List in Data Structure
  • Linked List in – C | C++ | Java
  • Singly Linked List in – C | C++ | Java
  • Insertion in singly Linked List – C | C++ | Java
    • Insertion at beginning in singly Linked List  – C | C++Java
    • Insertion at nth position in singly Linked List  – C | C++Java
    • Insertion at end in singly Linked List  – C | C++Java
  • Deletion in singly Linked List  – C | C++Java
    • Deletion from beginning in singly linked list : C | C++ | Java
    • Deletion from nth position in singly linked list : C | C++ | Java
    • Deletion from end in singly linked list : C | C++ | Java
  • Reverse a linked list without changing links between nodes (Data reverse only) – C | C++Java
  • Linked List Insertion and Deletion – C | C++Java
  • Reverse a linked list by changing links between nodes – C | C++Java
  • Linked List insertion in the middle – C | C++Java
  • Print reverse of a linked list without actually reversing – C |C++ | Java
  • Search an element in a linked list – C | C++Java
  • Insertion in a Sorted Linked List – C | C++Java
  • Delete alternate nodes of a Linked List – C | C++Java
  • Find middle of the linked list – C | C++Java
  • Reverse a linked list in groups of given size – C | C++Java
  • Find kth node from end of the linked list – C | C++Java
  • Append the last n nodes of a linked list to the beginning of the list – C | C++Java
  • Check whether linked list is palindrome or not – C | C++Java
  • Fold a Linked List – C | C++Java
  • Insert at a given position – C | C++Java
  • Delete at a given position – C | C++Java

Singly Linked List

  • Introduction to Linked List in Data Structure
    Click Here
  • Linked List in –
    C | C++ | Java
  • Singly Linked List in –
    C | C++ | Java
  • Insertion in singly Linked List –
    C | C++ | Java
  • Insertion at beginning in singly Linked List  –
    C | C++Java
  • Insertion at nth position in singly Linked List  –
    C | C++Java
  • Insertion at end in singly Linked List  –
    C | C++Java
  • Deletion in singly Linked List  –
    C | C++Java
  • Deletion from beginning in singly linked list :
    C | C++ | Java
  • Deletion from nth position in singly linked list :
    C | C++ | Java
  • Deletion from end in singly linked list :
    C | C++ | Java
  • Linked List Insertion and Deletion –
    C | C++Java
  • Reverse a linked list without changing links between nodes (Data reverse only) –
    C | C++Java
  • Reverse a linked list by changing links between nodes –
    C | C++Java
  • Print reverse of a linked list without actually reversing –
    C |C++Java
  • Print reverse of a linked list without actually reversing –
    C |C++Java
  • Insertion in the middle Singly Linked List –
    C | C++Java
  • Insertion in a Sorted Linked List –
    C | C++Java
  • Delete alternate nodes of a Linked List –
    C | C++Java
  • Find middle of the linked list –
    C | C++Java
  • Reverse a linked list in groups of given size –
    C | C++Java
  • Find kth node from end of the linked list –
    C | C++Java
  • Append the last n nodes of a linked list to the beginning of the list –
    C | C++Java
  • Check whether linked list is palindrome or not –
    C | C++Java
  • Fold a Linked List –
    C | C++Java
  • Insert at given Position –
    C | C++Java
  • Deletion at given Position –
    C | C++Java