Skip to content

Commit ec13675

Browse files
committed
https://leetcode.com/problems/merge-k-sorted-lists/
1 parent d314a42 commit ec13675

File tree

2 files changed

+51
-89
lines changed

2 files changed

+51
-89
lines changed

HardProblems/Merge k Sorted Lists.cs

Lines changed: 49 additions & 84 deletions
Original file line numberDiff line numberDiff line change
@@ -1,110 +1,75 @@
1-
using LeetCode.SharedUtils;
2-
3-
4-
namespace LeetCode.Hard
1+
namespace LeetCode.HardProblems
52
{
6-
//https://leetcode.com/problems/merge-k-sorted-lists/submissions/
7-
class Merge_k_Sorted_Lists
3+
class MergeKSortedLists
84
{
95
public ListNode MergeKLists(ListNode[] lists)
106
{
11-
if (lists != null && lists.Length > 0)
12-
{
13-
if (lists.Length == 1)
14-
{
15-
return lists[0];
16-
}
17-
else
18-
{
19-
var currNode = lists[0];
20-
21-
for (int i = 1; i < lists.Length; i++)
22-
{
23-
currNode = Merge(currNode, lists[i]);
24-
}
7+
// Handle edge cases
8+
if (lists == null || lists.Length == 0) return null;
259

26-
return currNode;
27-
}
28-
}
29-
else
30-
{
31-
return null;
32-
}
10+
// Start the divide-and-conquer process
11+
return MergeDivideAndConquer(lists, 0, lists.Length - 1);
3312
}
3413

35-
private ListNode Merge(ListNode node1, ListNode node2)
14+
private ListNode MergeDivideAndConquer(ListNode[] lists, int left, int right)
3615
{
37-
ListNode returnValue = new ListNode(0);
38-
var curr = returnValue;
16+
// Base case: only one list in the current range
17+
if (left == right) return lists[left];
3918

40-
while (node1 != null && node2 != null)
41-
{
42-
if (node1 != null && node2 != null)
43-
{
44-
if (node1.val < node2.val)
45-
{
46-
curr.next = node1;
47-
node1 = node1.next;
48-
}
49-
else
50-
{
51-
curr.next = node2;
52-
node2 = node2.next;
53-
}
54-
}
19+
// Divide the problem into two halves
20+
int mid = left + (right - left) / 2;
21+
ListNode leftList = MergeDivideAndConquer(lists, left, mid);
22+
ListNode rightList = MergeDivideAndConquer(lists, mid + 1, right);
5523

56-
curr = curr.next;
57-
}
58-
59-
if (node1 == null)
60-
{
61-
curr.next = node2;
62-
}
63-
64-
if (node2 == null)
65-
{
66-
curr.next = node1;
67-
}
68-
69-
return returnValue.next;
24+
// Merge the two halves
25+
return MergeTwoLists(leftList, rightList);
7026
}
7127

72-
public ListNode MergeKLists2(ListNode[] lists)
28+
private ListNode MergeTwoLists(ListNode l1, ListNode l2)
7329
{
74-
if (lists != null || lists.Count() > 0)
30+
// Create a dummy head to simplify edge cases
31+
ListNode dummy = new ListNode();
32+
ListNode current = dummy;
33+
34+
// Compare and link nodes in sorted order
35+
while (l1 != null && l2 != null)
7536
{
76-
List<int> vals = new List<int>();
77-
foreach (var item in lists)
37+
if (l1.val <= l2.val)
7838
{
79-
vals = Read(item, vals);
39+
current.next = l1;
40+
l1 = l1.next;
8041
}
81-
82-
var orderedList = vals.OrderBy(x => x).ToArray();
83-
84-
ListNode currNode = default, prevNode = default;
85-
for (int i = orderedList.Length - 1; i >= 0; i--)
42+
else
8643
{
87-
prevNode = currNode;
88-
currNode = new ListNode(orderedList[i], prevNode);
44+
current.next = l2;
45+
l2 = l2.next;
8946
}
9047

91-
return currNode;
92-
}
93-
else
94-
{
95-
return default;
48+
current = current.next;
9649
}
50+
51+
// Attach remaining nodes (if any)
52+
current.next = (l1 != null) ? l1 : l2;
53+
54+
return dummy.next;
9755
}
9856

99-
private List<int> Read(ListNode node, List<int> val)
100-
{
101-
if (node != null)
102-
{
103-
val.Add(node.val);
104-
val = Read(node.next, val);
105-
}
10657

107-
return val;
58+
[Test(Description = "https://leetcode.com/problems/merge-k-sorted-lists/")]
59+
[Category("Hard")]
60+
[Category("LeetCode")]
61+
[Category("Merge K Sorted Lists")]
62+
[TestCaseSource(nameof(Input))]
63+
public void Test1((int[] Output, int[][] Input) item)
64+
{
65+
var response = MergeKLists(item.Input.Select(x => x.ToListNode()).ToArray());
66+
Assert.That(response.ToArray(), Is.EqualTo(item.Output));
10867
}
68+
69+
public static IEnumerable<(int[] Output, int[][] Input)> Input =>
70+
new List<(int[] Output, int[][] Input)>()
71+
{
72+
([1, 1, 2, 3, 4, 4, 5, 6], ( [[1, 4, 5], [1, 3, 4], [2, 6]])),
73+
};
10974
}
11075
}

MediumProblems/Sort List.cs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,4 @@
1-
using LeetCode.SharedUtils;
2-
3-
4-
namespace LeetCode.MediumProblems
1+
namespace LeetCode.MediumProblems
52
{
63
class SortListSolution
74
{
@@ -75,7 +72,7 @@ private ListNode Merge(ListNode left, ListNode right)
7572
[Category("LeetCode")]
7673
[Category("Sort List")]
7774
[TestCaseSource(nameof(Input))]
78-
public void Test1((int?[] Output, int[] Input) item)
75+
public void Test1((int[] Output, int[] Input) item)
7976
{
8077
var response = SortList(item.Input.ToListNode());
8178
Assert.That(response.ToArray(), Is.EqualTo(item.Output));

0 commit comments

Comments
 (0)