jayati

Merge two sorted linked lists

Sep 6th, 2023
1,175
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.39 KB | None | 0 0
  1. //{ Driver Code Starts
  2. #include<iostream>
  3. using namespace std;
  4.  
  5. /* Link list Node */
  6. struct Node
  7. {
  8.     int data;
  9.     struct Node *next;
  10.    
  11.     Node(int x)
  12.     {
  13.         data = x;
  14.         next = NULL;
  15.     }
  16. };
  17.  
  18. Node* sortedMerge(struct Node* a, struct Node* b);
  19.  
  20. /* Function to print Nodes in a given linked list */
  21. void printList(struct Node *n)
  22. {
  23.     while (n!=NULL)
  24.     {
  25.         cout << n->data << " ";
  26.         n = n->next;
  27.     }
  28.     cout << endl;
  29. }
  30.  
  31. /* Driver program to test above function*/
  32. int main()
  33. {
  34.     int t;
  35.     cin>>t;
  36.     while(t--)
  37.     {
  38.         int n,m;
  39.         cin>>n>>m;
  40.  
  41.         int data;
  42.         cin>>data;
  43.         struct Node *head1 = new Node(data);
  44.         struct Node *tail1 = head1;
  45.         for (int i = 1; i < n; ++i)
  46.         {
  47.             cin>>data;
  48.             tail1->next = new Node(data);
  49.             tail1 = tail1->next;
  50.         }
  51.  
  52.         cin>>data;
  53.         struct Node *head2 = new Node(data);
  54.         struct Node *tail2 = head2;
  55.         for(int i=1; i<m; i++)
  56.         {
  57.             cin>>data;
  58.             tail2->next = new Node(data);
  59.             tail2 = tail2->next;
  60.         }
  61.  
  62.         Node *head = sortedMerge(head1, head2);
  63.         printList(head);
  64.     }
  65.     return 0;
  66. }
  67.  
  68. // } Driver Code Ends
  69.  
  70.  
  71.  
  72.  
  73. /* Link list Node
  74. struct Node {
  75.   int data;
  76.   struct Node *next;
  77.  
  78.   Node(int x) {
  79.     data = x;
  80.     next = NULL;
  81.   }
  82. };
  83. */
  84. //Function to merge two sorted linked list.
  85. Node* reverseList(Node* head)
  86. {
  87.     if (head->next == NULL)
  88.         return head;
  89.     Node* rest = reverseList(head->next);
  90.     head->next->next = head;
  91.     head->next = NULL;
  92.     return rest;
  93. }
  94.  
  95. // Given two non-empty linked lists 'a' and 'b'
  96. Node* sortedMerge(Node* a, Node* b)
  97. {
  98.     // Reverse Linked List 'a'
  99.     a = reverseList(a);
  100.     // Reverse Linked List 'b'
  101.     b = reverseList(b);
  102.     // Initialize head of resultant list
  103.     Node* head = NULL;
  104.     Node* temp;
  105.     // Traverse both lists while both of them
  106.     // have nodes.
  107.     while (a != NULL && b != NULL) {
  108.         // If a's current value is greater than or equal to
  109.         // b's current value.
  110.         if (a->data >= b->data) {
  111.             // Store next of current Node in first list
  112.             temp = a->next;
  113.             // Add 'a' at the front of resultant list
  114.             a->next = head;
  115.             // Make 'a' - head of the result list
  116.             head = a;
  117.             // Move ahead in first list
  118.             a = temp;
  119.         }
  120.  
  121.         // If b's value is greater. Below steps are similar
  122.         // to above (Only 'a' is replaced with 'b')
  123.         else {
  124.             temp = b->next;
  125.             b->next = head;
  126.             head = b;
  127.             b = temp;
  128.         }
  129.     }
  130.  
  131.     // If second list reached end, but first list has
  132.     // nodes. Add remaining nodes of first list at the
  133.     // beginning of result list
  134.     while (a != NULL) {
  135.         temp = a->next;
  136.         a->next = head;
  137.         head = a;
  138.         a = temp;
  139.     }
  140.  
  141.     // If first list reached end, but second list has
  142.     // nodes. Add remaining nodes of second list at the
  143.     // beginning of result list
  144.     while (b != NULL) {
  145.         temp = b->next;
  146.         b->next = head;
  147.         head = b;
  148.         b = temp;
  149.     }
  150.     // Return the head of the result list
  151.     return head;
  152. }
  153.  // https://practice.geeksforgeeks.org/problems/merge-two-sorted-linked-lists/
Advertisement
Add Comment
Please, Sign In to add comment