-
Notifications
You must be signed in to change notification settings - Fork 30
/
Copy pathMerge k Sorted Lists.cpp
85 lines (65 loc) · 2.22 KB
/
Merge k Sorted Lists.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
/*
Solution by Rahul Surana
***********************************************************
You are given an array of k linked-lists lists, each linked-list is sorted in ascending order.
Merge all the linked-lists into one sorted linked-list and return it.
***********************************************************
*/
#include <bits/stdc++.h>
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode() : val(0), next(nullptr) {}
* ListNode(int x) : val(x), next(nullptr) {}
* ListNode(int x, ListNode *next) : val(x), next(next) {}
* };
*/
class Solution {
public:
ListNode* merge(ListNode* first, ListNode* second)
{
ListNode* result = NULL;
if(first == nullptr)
return second;
if(second == nullptr)
return first;
if(first -> val <= second -> val)
{
result = first;
result -> next = merge(first -> next, second);
}
else
{
result = second;
result -> next = merge(first, second -> next);
}
return result;
}
ListNode* mergeKLists(vector<ListNode*>& arr) {
int k = arr.size(); // extracting size of array
if(k == 0) // if size of array is value
return NULL;
int start = 0; // start pointer
int last = k -1; // last pointer
int temp;
while(last != 0) // if last pointer not becomes zero
{
start = 0;
temp = last;
while(start < temp)
{
// merge them and store in one of the linked list
arr[start] = merge(arr[start],arr[temp]);
start++; // increment start
temp--; // decrese start
if(start >= temp) // if at any point start passes the temp
{
last = temp;
}
}
}
return arr[0]; // return first linked list of the aray as now it contains the all nodes in the sorted order.
}
};