23. Merge k Sorted Lists
This commit is contained in:
11
23. Merge k Sorted Lists/23. Merge k Sorted Lists.csproj
Normal file
11
23. Merge k Sorted Lists/23. Merge k Sorted Lists.csproj
Normal file
@@ -0,0 +1,11 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<OutputType>Exe</OutputType>
|
||||
<TargetFramework>net10.0</TargetFramework>
|
||||
<RootNamespace>_23._Merge_k_Sorted_Lists</RootNamespace>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<Nullable>enable</Nullable>
|
||||
</PropertyGroup>
|
||||
|
||||
</Project>
|
||||
11
23. Merge k Sorted Lists/ListNode.cs
Normal file
11
23. Merge k Sorted Lists/ListNode.cs
Normal file
@@ -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;
|
||||
}
|
||||
}
|
||||
2
23. Merge k Sorted Lists/Program.cs
Normal file
2
23. Merge k Sorted Lists/Program.cs
Normal file
@@ -0,0 +1,2 @@
|
||||
// See https://aka.ms/new-console-template for more information
|
||||
Console.WriteLine("Hello, World!");
|
||||
29
23. Merge k Sorted Lists/Solution.cs
Normal file
29
23. Merge k Sorted Lists/Solution.cs
Normal file
@@ -0,0 +1,29 @@
|
||||
public class Solution
|
||||
{
|
||||
public ListNode? MergeKLists(ListNode[] lists)
|
||||
{
|
||||
ListNode? head = null;
|
||||
ListNode? tail = null;
|
||||
var queue = new PriorityQueue<ListNode, int>();
|
||||
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;
|
||||
}
|
||||
}
|
||||
14
Leetcode.sln
14
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
|
||||
|
||||
Reference in New Issue
Block a user