-
Notifications
You must be signed in to change notification settings - Fork 30
/
Copy pathMergekSortedLists.cs
74 lines (68 loc) · 3.93 KB
/
MergekSortedLists.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
66
67
68
69
70
71
72
73
// Source : https://leetcode.com/problems/merge-k-sorted-lists/
// Author : codeyu
// Date : 2016年10月10日 20:20:15
/**********************************************************************************
*
*
* Merge k sorted linked lists and return it as one sorted list.
* Analyze and describe its complexity.
*
*
**********************************************************************************/
using System;
using System.Collections.Generic;
using Algorithms.Utils;
namespace Algorithms
{
/**
* Definition for singly-linked list.
* public class ListNode {
* public int val;
* public ListNode next;
* public ListNode(int x) { val = x; }
* }
*/
public class Solution023
{
public static ListNode<int> MergeKLists(ListNode<int>[] lists)
{
if(lists.Length == 0) return null;
var right = lists.Length - 1;
while(right > 0)
{
var left = 0;
while(left < right)
{
lists[left] = MergeTwoLists(lists[left],lists[right]);
left++;
right--;
}
}
return lists[0];
}
private static ListNode<int> MergeTwoLists(ListNode<int> l1, ListNode<int> l2)
{
ListNode<int> dummy = new ListNode<int>(0);
ListNode<int> lastNode = dummy;
while (l1 != null && l2 != null) {
if (l1.Val < l2.Val) {
lastNode.Next = l1;
l1 = l1.Next;
} else {
lastNode.Next = l2;
l2 = l2.Next;
}
lastNode = lastNode.Next;
}
if (l1 != null)
{
lastNode.Next = l1;
}
if (l2 != null)
{
lastNode.Next = l2;
}
return dummy.Next;
}
}
}