From 7210c68a2f68963029d9c4debabc8ecf24a28e74 Mon Sep 17 00:00:00 2001 From: jackfiled Date: Fri, 12 Jul 2024 11:33:53 +0800 Subject: [PATCH] 20240712 Finished --- LeetCodeSharp/Problems/Solution23.cs | 77 ++++++++++++++++++++++++++++ 1 file changed, 77 insertions(+) create mode 100644 LeetCodeSharp/Problems/Solution23.cs diff --git a/LeetCodeSharp/Problems/Solution23.cs b/LeetCodeSharp/Problems/Solution23.cs new file mode 100644 index 0000000..8fc2965 --- /dev/null +++ b/LeetCodeSharp/Problems/Solution23.cs @@ -0,0 +1,77 @@ +// [23] Merge k Sorted Lists + +using LeetCodeSharp.Utils; + +namespace LeetCodeSharp.Problems23 +{ + // Submission codes start here + + /** + * 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 + { + private ListNode MergeTwoLists(ListNode a, ListNode b) + { + if (a == null || b == null) + { + return a ?? b; + } + + var dummyHead = new ListNode(); + var node = dummyHead; + + while (a != null && b != null) + { + if (a.val < b.val) + { + node.next = a; + a = a.next; + } + else + { + node.next = b; + b = b.next; + } + + node = node.next; + } + + node.next = a ?? b; + + return dummyHead.next; + } + + private ListNode Merge(ListNode[] lists, int l, int r) + { + if (l == r) + { + return lists[l]; + } + + if (l > r) + { + return null; + } + + var middle = (l + r) / 2; + + return MergeTwoLists(Merge(lists, l, middle), Merge(lists, middle + 1, r)); + } + + public ListNode MergeKLists(ListNode[] lists) + { + return Merge(lists, 0, lists.Length - 1); + } + } + + // Submission codes end here +} \ No newline at end of file