Recursive approach for alternating split of Linked List in C++

C++Server Side ProgrammingProgramming

Given a Singly linked list as input. The goal is to split the list into two singly linked lists that have alternate nodes of the original list. If the input list has nodes a → b → c → d → e → f then after the split, two sub-lists will be a → c → e and b → d → f.

We will take two pointers N1 and N2 one pointing to the head of the original list and another pointing to the head → next. Now move both pointers to the next of next node and create sublists.

Examples

Input − List :- 1 → 5 → 7 → 12 → 2 → 96 → 33

Output − Original List : 1 5 7 12 2 96 33

List 1: 1 7 2 33

List 2: 5 12 96

Explanation − Start from 1 and 5 and next point to alternate nodes to create sublists shown above.

Input − List :- 13 → 53 → 90 → 18 → 44 → 11→ 99 → 32

Output − Original List : 13 53 90 18 44 11 99 32

List 1: 13 90 44 99

List 2: 53 18 11 32

Explanation − Start from 13 and 53 and next point to alternate nodes to create sublists shown above.

Approach used in the below program is as follows

In this approach we will take two pointers N1 and N2 one pointing to the head of the original list and another pointing to the head→ next. Now move both pointers to the next of next node and create sublists.

  • Take the structure Node with int data part and Node as next pointer.

  • Function addtohead(Node** head, int data) is used to add nodes to the head to create a singly linked list.

  • Create a singly linked list using the above function with head as pointer to first node.

  • Function display(Node* head) is used to print the linked list starting from head node.

  • Take two Node pointers node1 and node2.

  • Function splitList(Node* head, Node** n1, Node** n2) takes node pointers and points n1 to head and n2 to head → next of the original string.

  • Inside it call split(*n1, *n2) to split original list inot two sublists

  • Function split(Node* N1, Node* N2) takes N1 and N2 pointers and create two sublists containing alternating nodes of original list.

  • If both N1 and N2 are null then return nothing.

  • If N1→ next is not null then set tmp=N1->next->next and N1->next = tmp;

  • f N2→ next is not null then set tmp=N2->next->next and N2->next = tmp;

  • Call split(N1->next, N2->next); for next iteration.

  • At the end print sublists using display().

Example

#include <bits/stdc++.h>
using namespace std;
struct Node {
   int data;
   struct Node* next;
};
void addtohead(Node** head, int data){
   Node* nodex = new Node;
   nodex->data = data;
   nodex->next = (*head);
   (*head) = nodex;
}
void split(Node* N1, Node* N2){
   Node *tmp;
   if (N1 == NULL || N2 == NULL){
      return;
   }
   if (N1->next != NULL){
      tmp=N1->next->next;
      N1->next = tmp;
   }
   if (N2->next != NULL){
      tmp=N2->next->next;
      N2->next = tmp;
   }
   split(N1->next, N2->next);
}
void splitList(Node* head, Node** n1, Node** n2){
   *n1 = head;
   *n2 = head->next;
   split(*n1, *n2);
}
void display(Node* head){
   Node* curr = head;
   if (curr != NULL){
      cout<<curr->data<<" ";
      display(curr->next);
   }
}
int main(){
   Node* head = NULL;
   Node *node1 = NULL, *node2 = NULL;
   addtohead(&head, 20);
   addtohead(&head, 12);
   addtohead(&head, 15);
   addtohead(&head, 8);
   addtohead(&head, 10);
   addtohead(&head, 4);
   addtohead(&head, 5);

   cout<<"Original List :"<<endl;
   display(head);
   splitList(head, &node1, &node2);
   cout<<endl<<"List 1: ";
   display(node1);
   cout<<endl<<"List 2: ";
   display(node2);
   return 0;
}

Output

If we run the above code it will generate the following Output

Original List :
5 4 10 8 15 12 20
List 1: 5 10 15 20
List 2: 4 8 12
raja
Updated on 03-Nov-2021 06:56:48

Advertisements