23. Merge k Sorted Lists

This commit is contained in:
2026-03-25 17:32:12 +03:00
parent 2464aeff0e
commit 12912123de
5 changed files with 67 additions and 0 deletions

View 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>

View 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;
}
}

View File

@@ -0,0 +1,2 @@
// See https://aka.ms/new-console-template for more information
Console.WriteLine("Hello, World!");

View 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;
}
}

View File

@@ -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