diff --git a/23. Merge k Sorted Lists/23. Merge k Sorted Lists.csproj b/23. Merge k Sorted Lists/23. Merge k Sorted Lists.csproj new file mode 100644 index 0000000..e03a3cc --- /dev/null +++ b/23. Merge k Sorted Lists/23. Merge k Sorted Lists.csproj @@ -0,0 +1,11 @@ + + + + Exe + net10.0 + _23._Merge_k_Sorted_Lists + enable + enable + + + diff --git a/23. Merge k Sorted Lists/ListNode.cs b/23. Merge k Sorted Lists/ListNode.cs new file mode 100644 index 0000000..c1edb91 --- /dev/null +++ b/23. Merge k Sorted Lists/ListNode.cs @@ -0,0 +1,11 @@ + +public class ListNode +{ + public int val; + public ListNode? next; + public ListNode(int val = 0, ListNode? next = null) + { + this.val = val; + this.next = next; + } +} diff --git a/23. Merge k Sorted Lists/Program.cs b/23. Merge k Sorted Lists/Program.cs new file mode 100644 index 0000000..3751555 --- /dev/null +++ b/23. Merge k Sorted Lists/Program.cs @@ -0,0 +1,2 @@ +// See https://aka.ms/new-console-template for more information +Console.WriteLine("Hello, World!"); diff --git a/23. Merge k Sorted Lists/Solution.cs b/23. Merge k Sorted Lists/Solution.cs new file mode 100644 index 0000000..5aa8b1e --- /dev/null +++ b/23. Merge k Sorted Lists/Solution.cs @@ -0,0 +1,29 @@ +public class Solution +{ + public ListNode? MergeKLists(ListNode[] lists) + { + ListNode? head = null; + ListNode? tail = null; + var queue = new PriorityQueue(); + foreach (var l in lists) + if (l != null) + queue.Enqueue(l, l.val); + while (queue.Count > 0) + { + var l = queue.Dequeue(); + if (head == null || tail == null) + { + head = l; + tail = l; + } + else + { + tail.next = l; + tail = l; + } + if (l.next != null) + queue.Enqueue(l.next, l.next.val); + } + return head; + } +} \ No newline at end of file diff --git a/Leetcode.sln b/Leetcode.sln index 83f6d2b..69f90ff 100644 --- a/Leetcode.sln +++ b/Leetcode.sln @@ -185,6 +185,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "3546. Equal Sum Grid Partit EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "18. 4Sum", "18. 4Sum\18. 4Sum.csproj", "{0AF9E85C-7CCC-4E82-80B6-39931EA17D52}" EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "23. Merge k Sorted Lists", "23. Merge k Sorted Lists\23. Merge k Sorted Lists.csproj", "{B586286C-CBA7-4AF0-A2B3-E488639B2E5C}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -1287,6 +1289,18 @@ Global {0AF9E85C-7CCC-4E82-80B6-39931EA17D52}.Release|x64.Build.0 = Release|Any CPU {0AF9E85C-7CCC-4E82-80B6-39931EA17D52}.Release|x86.ActiveCfg = Release|Any CPU {0AF9E85C-7CCC-4E82-80B6-39931EA17D52}.Release|x86.Build.0 = Release|Any CPU + {B586286C-CBA7-4AF0-A2B3-E488639B2E5C}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {B586286C-CBA7-4AF0-A2B3-E488639B2E5C}.Debug|Any CPU.Build.0 = Debug|Any CPU + {B586286C-CBA7-4AF0-A2B3-E488639B2E5C}.Debug|x64.ActiveCfg = Debug|Any CPU + {B586286C-CBA7-4AF0-A2B3-E488639B2E5C}.Debug|x64.Build.0 = Debug|Any CPU + {B586286C-CBA7-4AF0-A2B3-E488639B2E5C}.Debug|x86.ActiveCfg = Debug|Any CPU + {B586286C-CBA7-4AF0-A2B3-E488639B2E5C}.Debug|x86.Build.0 = Debug|Any CPU + {B586286C-CBA7-4AF0-A2B3-E488639B2E5C}.Release|Any CPU.ActiveCfg = Release|Any CPU + {B586286C-CBA7-4AF0-A2B3-E488639B2E5C}.Release|Any CPU.Build.0 = Release|Any CPU + {B586286C-CBA7-4AF0-A2B3-E488639B2E5C}.Release|x64.ActiveCfg = Release|Any CPU + {B586286C-CBA7-4AF0-A2B3-E488639B2E5C}.Release|x64.Build.0 = Release|Any CPU + {B586286C-CBA7-4AF0-A2B3-E488639B2E5C}.Release|x86.ActiveCfg = Release|Any CPU + {B586286C-CBA7-4AF0-A2B3-E488639B2E5C}.Release|x86.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE