Skip to content

Commit 63e9c10

Browse files
author
Bogdan Hladiuc
committed
Reverse Linked List in Group of k
1 parent 22d947c commit 63e9c10

File tree

8 files changed

+234
-3
lines changed

8 files changed

+234
-3
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<ItemGroup>
4+
<ProjectReference Include="..\..\..\Library\Library.csproj" />
5+
</ItemGroup>
6+
7+
<PropertyGroup>
8+
<TargetFramework>net6.0</TargetFramework>
9+
<ImplicitUsings>enable</ImplicitUsings>
10+
<Nullable>enable</Nullable>
11+
</PropertyGroup>
12+
13+
</Project>
Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
#nullable disable warnings
2+
using System.Diagnostics;
3+
using Library;
4+
5+
namespace Formation {
6+
7+
public class ReverseKGroup {
8+
public static void Run() {
9+
var test = new Test {
10+
Id = 1,
11+
K = 2,
12+
List = new SingleLinkedListNode(1,
13+
new SingleLinkedListNode(2,
14+
new SingleLinkedListNode(3,
15+
new SingleLinkedListNode(4,
16+
new SingleLinkedListNode(5, null))))),
17+
Expected = new SingleLinkedListNode(2,
18+
new SingleLinkedListNode(1,
19+
new SingleLinkedListNode(4,
20+
new SingleLinkedListNode(3,
21+
new SingleLinkedListNode(5, null)))))
22+
};
23+
24+
test.List.Print();
25+
var result = Reverse(test.List, test.K);
26+
result.Print();
27+
test.Verify(result);
28+
}
29+
30+
private static SingleLinkedListNode ReverseKGroups(SingleLinkedListNode head, int k) {
31+
var curr = head;
32+
var count = 0;
33+
34+
while (curr != null && count < k) {
35+
count++;
36+
curr = curr.Next;
37+
}
38+
39+
if (count == k) {
40+
var reversedHead = ReverseLinkedList(head, k);
41+
head.Next = ReverseKGroups(curr, k);
42+
return reversedHead;
43+
}
44+
45+
return head;
46+
}
47+
48+
private static SingleLinkedListNode Reverse(SingleLinkedListNode head, int k) {
49+
SingleLinkedListNode newHead = null;
50+
SingleLinkedListNode tail = null;
51+
var ptr = head;
52+
53+
while (ptr != null) {
54+
var count = 0;
55+
ptr = head;
56+
57+
while (count < k && ptr != null) {
58+
ptr = ptr.Next;
59+
count++;
60+
}
61+
62+
if (count == k) {
63+
var revHead = ReverseLinkedList(head, k);
64+
65+
if (newHead == null) {
66+
newHead = revHead;
67+
}
68+
69+
if (tail != null) {
70+
tail.Next = revHead;
71+
}
72+
73+
tail = head;
74+
head = ptr;
75+
}
76+
}
77+
78+
if (tail != null) {
79+
tail.Next = head;
80+
}
81+
82+
return newHead ?? head;
83+
}
84+
85+
private static SingleLinkedListNode ReverseLinkedList(SingleLinkedListNode head, int k) {
86+
SingleLinkedListNode newHead = null;
87+
var curr = head;
88+
89+
while (k > 0) {
90+
var nextNode = curr.Next;
91+
curr.Next = newHead;
92+
newHead = curr;
93+
curr = nextNode;
94+
k--;
95+
}
96+
97+
return newHead;
98+
}
99+
}
100+
101+
internal class Test {
102+
public int Id { get; set; }
103+
public int K { get; set; }
104+
public SingleLinkedListNode List { get; set; }
105+
public SingleLinkedListNode Expected { get; set; }
106+
107+
public void Verify(SingleLinkedListNode result) {
108+
Debug.Assert(CheckList(result), $"Test {Id} failed.");
109+
}
110+
111+
private bool CheckList(SingleLinkedListNode result) {
112+
while (Expected != null && result != null && Expected.Val == result.Val) {
113+
Expected = Expected.Next;
114+
result = result.Next;
115+
}
116+
117+
return Expected == null && result == null;
118+
}
119+
}
120+
}

LeetcodeNew.sln

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Day_1_Remove_Delete_Middle_
8989
EndProject
9090
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Day_2_Spiral_Traversal_Q54", "Formation\21_Days_Challenge\Day_2_Spiral_Traversal_Q54\Day_2_Spiral_Traversal_Q54.csproj", "{82FE764B-4AA7-4694-B9BC-85119629AF56}"
9191
EndProject
92+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Day_3_Reverse_Groups_of_K_Nodes_Q25", "Formation\21_Days_Challenge\Day_3_Reverse_Groups_of_K_Nodes_Q25\Day_3_Reverse_Groups_of_K_Nodes_Q25.csproj", "{4936F68E-2FB8-4DC3-96CF-53116D2437E6}"
93+
EndProject
9294
Global
9395
GlobalSection(SolutionConfigurationPlatforms) = preSolution
9496
Debug|Any CPU = Debug|Any CPU
@@ -594,10 +596,23 @@ Global
594596
{82FE764B-4AA7-4694-B9BC-85119629AF56}.Release|x64.Build.0 = Release|Any CPU
595597
{82FE764B-4AA7-4694-B9BC-85119629AF56}.Release|x86.ActiveCfg = Release|Any CPU
596598
{82FE764B-4AA7-4694-B9BC-85119629AF56}.Release|x86.Build.0 = Release|Any CPU
599+
{4936F68E-2FB8-4DC3-96CF-53116D2437E6}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
600+
{4936F68E-2FB8-4DC3-96CF-53116D2437E6}.Debug|Any CPU.Build.0 = Debug|Any CPU
601+
{4936F68E-2FB8-4DC3-96CF-53116D2437E6}.Debug|x64.ActiveCfg = Debug|Any CPU
602+
{4936F68E-2FB8-4DC3-96CF-53116D2437E6}.Debug|x64.Build.0 = Debug|Any CPU
603+
{4936F68E-2FB8-4DC3-96CF-53116D2437E6}.Debug|x86.ActiveCfg = Debug|Any CPU
604+
{4936F68E-2FB8-4DC3-96CF-53116D2437E6}.Debug|x86.Build.0 = Debug|Any CPU
605+
{4936F68E-2FB8-4DC3-96CF-53116D2437E6}.Release|Any CPU.ActiveCfg = Release|Any CPU
606+
{4936F68E-2FB8-4DC3-96CF-53116D2437E6}.Release|Any CPU.Build.0 = Release|Any CPU
607+
{4936F68E-2FB8-4DC3-96CF-53116D2437E6}.Release|x64.ActiveCfg = Release|Any CPU
608+
{4936F68E-2FB8-4DC3-96CF-53116D2437E6}.Release|x64.Build.0 = Release|Any CPU
609+
{4936F68E-2FB8-4DC3-96CF-53116D2437E6}.Release|x86.ActiveCfg = Release|Any CPU
610+
{4936F68E-2FB8-4DC3-96CF-53116D2437E6}.Release|x86.Build.0 = Release|Any CPU
597611
EndGlobalSection
598612
GlobalSection(NestedProjects) = preSolution
599613
{45EB27A3-9FBB-48ED-8C89-E0F074C02E8C} = {71C7D88B-6D5D-4E25-A275-869603700831}
600614
{F4D0F4C2-26A6-43AC-BC80-D1762684A605} = {45EB27A3-9FBB-48ED-8C89-E0F074C02E8C}
601615
{82FE764B-4AA7-4694-B9BC-85119629AF56} = {45EB27A3-9FBB-48ED-8C89-E0F074C02E8C}
616+
{4936F68E-2FB8-4DC3-96CF-53116D2437E6} = {45EB27A3-9FBB-48ED-8C89-E0F074C02E8C}
602617
EndGlobalSection
603618
EndGlobal

Run/Program.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,9 @@ static void Main(string[] args) {
44

55
#region Formation
66

7-
//Formation.DeleteMiddleNode.Run(); // Day 1
8-
Formation.SpiralTraversal.Run(); // Day 2
7+
// Formation.DeleteMiddleNode.Run(); // Day 1
8+
// Formation.SpiralTraversal.Run(); // Day 2
9+
Formation.ReverseKGroup.Run(); // Day 3
910

1011
#endregion Formation
1112

Run/Run.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
<ProjectReference Include="..\Q242_Valid_Anagram\Q242_Valid_Anagram.csproj" />
4343
<ProjectReference Include="..\Formation\21_Days_Challenge\Day_1_Remove_Delete_Middle_Node_Q2095\Day_1_Remove_Delete_Middle_Node_Q2095.csproj" />
4444
<ProjectReference Include="..\Formation\21_Days_Challenge\Day_2_Spiral_Traversal_Q54\Day_2_Spiral_Traversal_Q54.csproj" />
45+
<ProjectReference Include="..\Formation\21_Days_Challenge\Day_3_Reverse_Groups_of_K_Nodes_Q25\Day_3_Reverse_Groups_of_K_Nodes_Q25.csproj" />
4546
</ItemGroup>
4647

4748
<PropertyGroup>

Run/obj/Run.csproj.nuget.dgspec.json

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,62 @@
172172
}
173173
}
174174
},
175+
"/Users/Bogdan/Documents/Work/CtCI/VS_Code/LeetcodeNew/Formation/21_Days_Challenge/Day_3_Reverse_Groups_of_K_Nodes_Q25/Day_3_Reverse_Groups_of_K_Nodes_Q25.csproj": {
176+
"version": "1.0.0",
177+
"restore": {
178+
"projectUniqueName": "/Users/Bogdan/Documents/Work/CtCI/VS_Code/LeetcodeNew/Formation/21_Days_Challenge/Day_3_Reverse_Groups_of_K_Nodes_Q25/Day_3_Reverse_Groups_of_K_Nodes_Q25.csproj",
179+
"projectName": "Day_3_Reverse_Groups_of_K_Nodes_Q25",
180+
"projectPath": "/Users/Bogdan/Documents/Work/CtCI/VS_Code/LeetcodeNew/Formation/21_Days_Challenge/Day_3_Reverse_Groups_of_K_Nodes_Q25/Day_3_Reverse_Groups_of_K_Nodes_Q25.csproj",
181+
"packagesPath": "/Users/Bogdan/.nuget/packages/",
182+
"outputPath": "/Users/Bogdan/Documents/Work/CtCI/VS_Code/LeetcodeNew/Formation/21_Days_Challenge/Day_3_Reverse_Groups_of_K_Nodes_Q25/obj/",
183+
"projectStyle": "PackageReference",
184+
"configFilePaths": [
185+
"/Users/Bogdan/.nuget/NuGet/NuGet.Config"
186+
],
187+
"originalTargetFrameworks": [
188+
"net6.0"
189+
],
190+
"sources": {
191+
"https://api.nuget.org/v3/index.json": {}
192+
},
193+
"frameworks": {
194+
"net6.0": {
195+
"targetAlias": "net6.0",
196+
"projectReferences": {
197+
"/Users/Bogdan/Documents/Work/CtCI/VS_Code/LeetcodeNew/Library/Library.csproj": {
198+
"projectPath": "/Users/Bogdan/Documents/Work/CtCI/VS_Code/LeetcodeNew/Library/Library.csproj"
199+
}
200+
}
201+
}
202+
},
203+
"warningProperties": {
204+
"warnAsError": [
205+
"NU1605"
206+
]
207+
}
208+
},
209+
"frameworks": {
210+
"net6.0": {
211+
"targetAlias": "net6.0",
212+
"imports": [
213+
"net461",
214+
"net462",
215+
"net47",
216+
"net471",
217+
"net472",
218+
"net48"
219+
],
220+
"assetTargetFallback": true,
221+
"warn": true,
222+
"frameworkReferences": {
223+
"Microsoft.NETCore.App": {
224+
"privateAssets": "all"
225+
}
226+
},
227+
"runtimeIdentifierGraphPath": "/usr/local/share/dotnet/sdk/6.0.202/RuntimeIdentifierGraph.json"
228+
}
229+
}
230+
},
175231
"/Users/Bogdan/Documents/Work/CtCI/VS_Code/LeetcodeNew/Library/Library.csproj": {
176232
"version": "1.0.0",
177233
"restore": {
@@ -2263,6 +2319,9 @@
22632319
"/Users/Bogdan/Documents/Work/CtCI/VS_Code/LeetcodeNew/Formation/21_Days_Challenge/Day_2_Spiral_Traversal_Q54/Day_2_Spiral_Traversal_Q54.csproj": {
22642320
"projectPath": "/Users/Bogdan/Documents/Work/CtCI/VS_Code/LeetcodeNew/Formation/21_Days_Challenge/Day_2_Spiral_Traversal_Q54/Day_2_Spiral_Traversal_Q54.csproj"
22652321
},
2322+
"/Users/Bogdan/Documents/Work/CtCI/VS_Code/LeetcodeNew/Formation/21_Days_Challenge/Day_3_Reverse_Groups_of_K_Nodes_Q25/Day_3_Reverse_Groups_of_K_Nodes_Q25.csproj": {
2323+
"projectPath": "/Users/Bogdan/Documents/Work/CtCI/VS_Code/LeetcodeNew/Formation/21_Days_Challenge/Day_3_Reverse_Groups_of_K_Nodes_Q25/Day_3_Reverse_Groups_of_K_Nodes_Q25.csproj"
2324+
},
22662325
"/Users/Bogdan/Documents/Work/CtCI/VS_Code/LeetcodeNew/Library/Library.csproj": {
22672326
"projectPath": "/Users/Bogdan/Documents/Work/CtCI/VS_Code/LeetcodeNew/Library/Library.csproj"
22682327
},

Run/obj/project.assets.json

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,19 @@
4141
"bin/placeholder/Day_2_Spiral_Traversal_Q54.dll": {}
4242
}
4343
},
44+
"Day_3_Reverse_Groups_of_K_Nodes_Q25/1.0.0": {
45+
"type": "project",
46+
"framework": ".NETCoreApp,Version=v6.0",
47+
"dependencies": {
48+
"Library": "1.0.0"
49+
},
50+
"compile": {
51+
"bin/placeholder/Day_3_Reverse_Groups_of_K_Nodes_Q25.dll": {}
52+
},
53+
"runtime": {
54+
"bin/placeholder/Day_3_Reverse_Groups_of_K_Nodes_Q25.dll": {}
55+
}
56+
},
4457
"Library/1.0.0": {
4558
"type": "project",
4659
"framework": ".NETCoreApp,Version=v6.0",
@@ -502,6 +515,11 @@
502515
"path": "../Formation/21_Days_Challenge/Day_2_Spiral_Traversal_Q54/Day_2_Spiral_Traversal_Q54.csproj",
503516
"msbuildProject": "../Formation/21_Days_Challenge/Day_2_Spiral_Traversal_Q54/Day_2_Spiral_Traversal_Q54.csproj"
504517
},
518+
"Day_3_Reverse_Groups_of_K_Nodes_Q25/1.0.0": {
519+
"type": "project",
520+
"path": "../Formation/21_Days_Challenge/Day_3_Reverse_Groups_of_K_Nodes_Q25/Day_3_Reverse_Groups_of_K_Nodes_Q25.csproj",
521+
"msbuildProject": "../Formation/21_Days_Challenge/Day_3_Reverse_Groups_of_K_Nodes_Q25/Day_3_Reverse_Groups_of_K_Nodes_Q25.csproj"
522+
},
505523
"Library/1.0.0": {
506524
"type": "project",
507525
"path": "../Library/Library.csproj",
@@ -698,6 +716,7 @@
698716
"Counting_Sort >= 1.0.0",
699717
"Day_1_Remove_Delete_Middle_Node_Q2095 >= 1.0.0",
700718
"Day_2_Spiral_Traversal_Q54 >= 1.0.0",
719+
"Day_3_Reverse_Groups_of_K_Nodes_Q25 >= 1.0.0",
701720
"Library >= 1.0.0",
702721
"Q10_Regular_Expression_Matching >= 1.0.0",
703722
"Q1249_Minimum_Remove_To_Make_Valid_Parentheses >= 1.0.0",
@@ -772,6 +791,9 @@
772791
"/Users/Bogdan/Documents/Work/CtCI/VS_Code/LeetcodeNew/Formation/21_Days_Challenge/Day_2_Spiral_Traversal_Q54/Day_2_Spiral_Traversal_Q54.csproj": {
773792
"projectPath": "/Users/Bogdan/Documents/Work/CtCI/VS_Code/LeetcodeNew/Formation/21_Days_Challenge/Day_2_Spiral_Traversal_Q54/Day_2_Spiral_Traversal_Q54.csproj"
774793
},
794+
"/Users/Bogdan/Documents/Work/CtCI/VS_Code/LeetcodeNew/Formation/21_Days_Challenge/Day_3_Reverse_Groups_of_K_Nodes_Q25/Day_3_Reverse_Groups_of_K_Nodes_Q25.csproj": {
795+
"projectPath": "/Users/Bogdan/Documents/Work/CtCI/VS_Code/LeetcodeNew/Formation/21_Days_Challenge/Day_3_Reverse_Groups_of_K_Nodes_Q25/Day_3_Reverse_Groups_of_K_Nodes_Q25.csproj"
796+
},
775797
"/Users/Bogdan/Documents/Work/CtCI/VS_Code/LeetcodeNew/Library/Library.csproj": {
776798
"projectPath": "/Users/Bogdan/Documents/Work/CtCI/VS_Code/LeetcodeNew/Library/Library.csproj"
777799
},

Run/obj/project.nuget.cache

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"version": 2,
3-
"dgSpecHash": "7mK7UU9psheL5hPMTdwn8o8O/MHhimPYVgMIXsagy5QQn+hY5IeTPhQF40FoW9yXOKqJvcfeWGi3j59IL4UnHg==",
3+
"dgSpecHash": "PExtQsV6jTLun2CfiLPD9yz1kLS7Hk4gTRIUiljlED5qv3OIiL3AuVeIKs/t6R094YBHKmxVO46LAHmgfXk/PQ==",
44
"success": true,
55
"projectFilePath": "/Users/Bogdan/Documents/Work/CtCI/VS_Code/LeetcodeNew/Run/Run.csproj",
66
"expectedPackageFiles": [],

0 commit comments

Comments
 (0)