jayati

Flattening a Linked List

Sep 3rd, 2023
1,196
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.42 KB | None | 0 0
  1. //{ Driver Code Starts
  2. #include <bits/stdc++.h>
  3.  
  4. struct Node{
  5.     int data;
  6.     struct Node * next;
  7.     struct Node * bottom;
  8.    
  9.     Node(int x){
  10.         data = x;
  11.         next = NULL;
  12.         bottom = NULL;
  13.     }
  14.    
  15. };
  16.  
  17. using namespace std;
  18.  
  19. void printList(Node *Node)
  20. {
  21.     while (Node != NULL)
  22.     {
  23.         printf("%d ", Node->data);
  24.         Node = Node->bottom;
  25.     }
  26. }
  27.  
  28. Node* flatten (Node* root);
  29.  
  30. int main(void) {
  31.  
  32.     int t;
  33.     cin>>t;
  34.     while(t--){
  35.     int n,m,flag=1,flag1=1;
  36.     struct Node * temp=NULL;
  37.     struct Node * head=NULL;
  38.     struct Node * pre=NULL;
  39.     struct Node * tempB=NULL;
  40.     struct Node * preB=NULL;
  41.         cin>>n;
  42.         int work[n];
  43.         for(int i=0;i<n;i++)
  44.             cin>>work[i];
  45.         for(int i=0;i<n;i++){
  46.             m=work[i];
  47.             --m;
  48.             int data;
  49.             scanf("%d",&data);
  50.             temp = new Node(data);
  51.             temp->next = NULL;
  52.             temp->bottom = NULL;
  53.            
  54.             if(flag){
  55.                 head = temp;
  56.                 pre = temp;
  57.                 flag = 0;
  58.                 flag1 = 1;
  59.             }
  60.             else{
  61.                 pre->next = temp;
  62.                 pre = temp;
  63.                 flag1 = 1;
  64.             }
  65.             for(int j=0;j<m;j++){
  66.                
  67.                 int temp_data;
  68.                 scanf("%d",&temp_data);
  69.                 tempB = new Node(temp_data);
  70.  
  71.                 if(flag1){
  72.                     temp->bottom=tempB;
  73.                     preB=tempB;
  74.                     flag1=0;
  75.                 }
  76.                 else{
  77.                     preB->bottom=tempB;
  78.                     preB=tempB;
  79.                 }
  80.             }
  81.         }
  82.            Node *fun = head;
  83.            Node *fun2=head;
  84.  
  85.             Node* root = flatten(head);
  86.             printList(root);
  87.             cout<<endl;
  88.  
  89.     }
  90.     return 0;
  91. }
  92.  
  93. // } Driver Code Ends
  94.  
  95.  
  96. /* Node structure  used in the program
  97.  
  98. struct Node{
  99.     int data;
  100.     struct Node * next;
  101.     struct Node * bottom;
  102.    
  103.     Node(int x){
  104.         data = x;
  105.         next = NULL;
  106.         bottom = NULL;
  107.     }
  108.    
  109. };
  110. */
  111.  
  112. /*  Function which returns the  root of
  113.     the flattened linked list. */
  114.    
  115. Node* Merge(Node* a,Node* b)
  116. {
  117.     if(a==NULL)
  118.     {
  119.         return b;
  120.     }
  121.     if(b==NULL)
  122.     {
  123.         return a;
  124.     }
  125.     Node* result;
  126.     if(a->data < b->data)
  127.     {
  128.         result=a;
  129.         result->bottom = Merge(a->bottom,b);
  130.     }
  131.     else
  132.     {
  133.         result=b;
  134.         result->bottom = Merge(a,b->bottom);
  135.     }
  136.     result->next=NULL;
  137.     return result;
  138. }
  139.  
  140. Node *flatten(Node *root)
  141. {
  142.    // Your code here  
  143.    if(root==NULL || root->next==NULL)
  144.    {
  145.        return root;
  146.    }
  147.    root->next=flatten(root->next);
  148.    
  149.    root=Merge(root,root->next);
  150.    return root;
  151. }
  152. // https://practice.geeksforgeeks.org/problems/flattening-a-linked-list/
Advertisement
Add Comment
Please, Sign In to add comment