-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
eeb801a
commit a3a4d89
Showing
9 changed files
with
139 additions
and
74 deletions.
There are no files selected for viewing
10 changes: 10 additions & 0 deletions
10
LeetCode/src/LeetCode.Challenges/Problems0XX/P019_RemoveNthNodeFromTheEndOfList/ListNode.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
using System.Diagnostics.CodeAnalysis; | ||
|
||
namespace LeetCode.Challenges.Problems0XX.P019_RemoveNthNodeFromTheEndOfList; | ||
|
||
[ExcludeFromCodeCoverage] | ||
public class ListNode(int val = default, ListNode? next = null) | ||
{ | ||
public int Value { get; set; } = val; | ||
public ListNode? Next { get; set; } = next; | ||
} |
89 changes: 89 additions & 0 deletions
89
.../LeetCode.Challenges/Problems0XX/P019_RemoveNthNodeFromTheEndOfList/TwoPointerSolution.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
namespace LeetCode.Challenges.Problems0XX.P019_RemoveNthNodeFromTheEndOfList; | ||
|
||
// Time Complexity: O(n). | ||
// Space Complexity: O(1). | ||
public static class TwoPointerSolution | ||
{ | ||
public static ListNode RemoveNthFromEnd(ListNode head, int n) | ||
{ | ||
// For example, we need to remove the 2nd node from the end of this list: | ||
// 1 --> 2 --> 3 --> 4 --> 5 --> null | ||
|
||
ListNode dummy = new ListNode(default, head); | ||
ListNode left = dummy; | ||
ListNode? right = head; | ||
|
||
// dummy --> 1 --> 2 --> 3 --> 4 --> 5 --> null | ||
// ^ ^ | ||
// (left) ^ | ||
// (right) | ||
|
||
// Find where the right pointer should point to. | ||
while (n > 0 && right != null) | ||
{ | ||
right = right.Next; | ||
n--; | ||
} | ||
|
||
// The 1st iteration, n is 2 (n > 0 && right IS NOT null): | ||
// dummy --> 1 --> 2 --> 3 --> 4 --> 5 --> null | ||
// ^ ^ | ||
// (left) ^ | ||
// (right) | ||
|
||
// The 2nd iteration, n is 1 (n > 0 && right IS NOT null): | ||
// dummy --> 1 --> 2 --> 3 --> 4 --> 5 --> null | ||
// ^ ^ | ||
// (left) ^ | ||
// (right) | ||
|
||
// The loop stops processing it because n is 0. | ||
// Now, we have the following picture: | ||
// dummy --> 1 --> 2 --> 3 --> 4 --> 5 --> null | ||
// ^ ^ | ||
// (left) ^ | ||
// (right) | ||
|
||
// Move both pointers forward until the right one is null. | ||
// When the right pointer points to null, the left one points to the node before the one that should be removed. | ||
while (right != null) | ||
{ | ||
left = left.Next!; | ||
right = right.Next; | ||
} | ||
|
||
// The 1st iteration (before the iteration, right IS NOT null). | ||
// Thus, we can move the pointers. | ||
// dummy --> 1 --> 2 --> 3 --> 4 --> 5 --> null | ||
// ^ ^ | ||
// (left) ^ | ||
// (right) | ||
|
||
// The 2nd iteration (before the iteration, right IS NOT null). | ||
// Thus, we can move the pointers. | ||
// dummy --> 1 --> 2 --> 3 --> 4 --> 5 --> null | ||
// ^ ^ | ||
// (left) ^ | ||
// (right) | ||
|
||
// The 3rd iteration (before the iteration, right IS NOT null). | ||
// Thus, we can move the pointers. | ||
// dummy --> 1 --> 2 --> 3 --> 4 --> 5 --> null | ||
// ^ ^ | ||
// (left) ^ | ||
// (right) | ||
|
||
// The loop stops processing it because right IS null. | ||
// So, 'left.next' exactly points to the node to be removed. | ||
|
||
// Let's update the pointers. | ||
// Before: | ||
// 1 --> 2 --> 3 --> 4 --> 5 --> null | ||
left.Next = left.Next?.Next; | ||
|
||
// After: | ||
// 1 --> 2 --> 3 --> 5 --> null | ||
|
||
return dummy.Next!; | ||
} | ||
} |
15 changes: 6 additions & 9 deletions
15
LeetCode/src/LeetCode.Challenges/Problems0XX/P024_SwapNodesInPairs/ListNode.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,10 @@ | ||
using System.Diagnostics.CodeAnalysis; | ||
|
||
namespace LeetCode.Challenges.Problems0XX.P024_SwapNodesInPairs; | ||
|
||
public class ListNode | ||
[ExcludeFromCodeCoverage] | ||
public class ListNode(int value = default, ListNode? next = null) | ||
{ | ||
public int val; | ||
public ListNode? next; | ||
|
||
public ListNode(int val = 0, ListNode? next = null) | ||
{ | ||
this.val = val; | ||
this.next = next; | ||
} | ||
public int Value { get; set; } = value; | ||
public ListNode? Next { get; set; } = next; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
7 changes: 0 additions & 7 deletions
7
LeetCode/src/LeetCode.Challenges/RemoveNthNodeFromListEnd/ListNode.cs
This file was deleted.
Oops, something went wrong.
29 changes: 0 additions & 29 deletions
29
LeetCode/src/LeetCode.Challenges/RemoveNthNodeFromListEnd/TwoPointerSolution.cs
This file was deleted.
Oops, something went wrong.
12 changes: 9 additions & 3 deletions
12
...ests/RemoveNthNodeFromListEnd/TestData.cs → ...RemoveNthNodeFromTheEndOfList/TestData.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
16 changes: 16 additions & 0 deletions
16
...enges.UnitTests/Problems0XX/P019_RemoveNthNodeFromTheEndOfList/TwoPointerSolutionTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
using LeetCode.Challenges.Problems0XX.P019_RemoveNthNodeFromTheEndOfList; | ||
using Shouldly; | ||
using Xunit; | ||
|
||
namespace LeetCode.Challenges.UnitTests.Problems0XX.P019_RemoveNthNodeFromTheEndOfList; | ||
|
||
public class TwoPointerSolutionTests | ||
{ | ||
[Theory] | ||
[ClassData(typeof(TestData))] | ||
public void GivenListAndNodeNumber_WhenRemoveNthFromEnd_ThenResultAsExpected( | ||
ListNode head, int n, ListNode? expectedResult) | ||
{ | ||
TwoPointerSolution.RemoveNthFromEnd(head, n).ShouldBeEquivalentTo(expectedResult); | ||
} | ||
} |
17 changes: 0 additions & 17 deletions
17
...e/tests/LeetCode.Challenges.UnitTests/RemoveNthNodeFromListEnd/TwoPointerSolutionTests.cs
This file was deleted.
Oops, something went wrong.