• Source
    1. #include <bits/stdc++.h>
    2. using namespace std;
    3.  
    4. // A Linked List node
    5. struct Node
    6. {
    7. int data;
    8. Node* next;
    9. };
    10.  
    11. /* Function to print nodes in a given linked list */
    12. void printList(Node* node)
    13. {
    14. while (node != NULL)
    15. {
    16. printf("%d ", node->data);
    17. node = node->next;
    18. }
    19. }
    20.  
    21. /* Takes two lists sorted in increasing order, and merge
    22.   their nodes together to make one big sorted list. Below
    23.   function takes O(Log n) extra space for recursive calls,
    24.   but it can be easily modified to work with same time and
    25.   O(1) extra space */
    26. Node* SortedMerge(Node* a, Node* b)
    27. {
    28. Node* result = NULL;
    29.  
    30. /* Base cases */
    31. if (a == NULL)
    32. return (b);
    33. else if(b == NULL)
    34. return (a);
    35.  
    36. /* Pick either a or b, and recur */
    37. if(a->data <= b->data)
    38. {
    39. result = a;
    40. result->next = SortedMerge(a->next, b);
    41. }
    42. else
    43. {
    44. result = b;
    45. result->next = SortedMerge(a, b->next);
    46. }
    47.  
    48. return result;
    49. }
    50.  
    51. // The main function that takes an array of lists
    52. // arr[0..last] and generates the sorted output
    53. Node* mergeKLists(Node* arr[], int last)
    54. {
    55. // repeat until only one list is left
    56. while (last != 0)
    57. {
    58. int i = 0, j = last;
    59.  
    60. // (i, j) forms a pair
    61. while (i < j)
    62. {
    63. // merge List i with List j and store
    64. // merged list in List i
    65. arr[i] = SortedMerge(arr[i], arr[j]);
    66.  
    67. // consider next pair
    68. i++, j--;
    69.  
    70. // If all pairs are merged, update last
    71. if (i >= j)
    72. last = j;
    73. }
    74. }
    75.  
    76. return arr[0];
    77. }
    78.  
    79. // Utility function to create a new node.
    80. Node *newNode(int data)
    81. {
    82. struct Node *temp = new Node;
    83. temp->data = data;
    84. temp->next = NULL;
    85. return temp;
    86. }
    87.  
    88. // Driver program to test above functions
    89. int main()
    90. {
    91. int k = 3; // Number of linked lists
    92. int n = 4; // Number of elements in each list
    93.  
    94. // an array of pointers storing the head nodes
    95. // of the linked lists
    96. Node* arr[k];
    97.  
    98. arr[0] = newNode(1);
    99. arr[0]->next = newNode(3);
    100. arr[0]->next->next = newNode(5);
    101. arr[0]->next->next->next = newNode(7);
    102.  
    103. arr[1] = newNode(2);
    104. arr[1]->next = newNode(4);
    105. arr[1]->next->next = newNode(6);
    106. arr[1]->next->next->next = newNode(8);
    107.  
    108. arr[2] = newNode(0);
    109. arr[2]->next = newNode(9);
    110. arr[2]->next->next = newNode(10);
    111. arr[2]->next->next->next = newNode(11);
    112.  
    113. // Merge all lists
    114. Node* head = mergeKLists(arr, k - 1);
    115.  
    116. printList(head);
    117.  
    118. return 0;
    119. }