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