forked from neetcode-gh/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
0023-merge-k-sorted-lists.cs
65 lines (58 loc) · 1.48 KB
/
0023-merge-k-sorted-lists.cs
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
/**
* Definition for singly-linked list.
* public class ListNode {
* public int val;
* public ListNode next;
* public ListNode(int val=0, ListNode next=null) {
* this.val = val;
* this.next = next;
* }
* }
*/
public class Solution {
public ListNode MergeKLists(ListNode[] lists) {
if (lists.Length == 0)
{
return null;
}
while (lists.Length > 1)
{
var mergedLists = new ListNode[(lists.Length + 1) / 2];
for (int i = 0; i < lists.Length; i += 2)
{
var l1 = lists[i];
var l2 = (i + 1 < lists.Length) ? lists[i + 1] : null;
mergedLists[i/2] = (MergeLists(l1, l2));
}
lists = mergedLists;
}
return lists[0];
}
public ListNode MergeLists(ListNode l1, ListNode l2)
{
var sorted = new ListNode();
var current = sorted;
while (l1 != null && l2 != null)
{
if (l1.val <= l2.val)
{
current.next = l1;
l1 = l1.next;
}
else
{
current.next = l2;
l2 = l2.next;
}
current = current.next;
}
if (l1 != null)
{
current.next = l1;
} else
{
current.next = l2;
}
return sorted.next;
}
}