-
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.
Solve LeetCode #141 'Linked List Cycle' [Easy].
- Loading branch information
1 parent
31a9689
commit 9654bb8
Showing
4 changed files
with
143 additions
and
0 deletions.
There are no files selected for viewing
8 changes: 8 additions & 0 deletions
8
LeetCode/src/LeetCode.Challenges/Problems1XX/P141_LinkedListCycle/Description.md
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,8 @@ | ||
# Linked List Cycle (#141) | ||
|
||
Given head, the head of a linked list, determine if the linked list has a cycle in it. | ||
|
||
There is a cycle in a linked list if there is some node in the list | ||
that can be reached again by continuously following the next pointer. | ||
|
||
Return `true` if there is a cycle in the linked list. Otherwise, return `false`. |
7 changes: 7 additions & 0 deletions
7
LeetCode/src/LeetCode.Challenges/Problems1XX/P141_LinkedListCycle/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,7 @@ | ||
namespace LeetCode.Challenges.Problems1XX.P141_LinkedListCycle; | ||
|
||
public class ListNode(int x) | ||
{ | ||
public int Value { get; set; } = x; | ||
public ListNode? Next { get; set; } = null; | ||
} |
28 changes: 28 additions & 0 deletions
28
LeetCode/src/LeetCode.Challenges/Problems1XX/P141_LinkedListCycle/SlowFastPointerApproach.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,28 @@ | ||
namespace LeetCode.Challenges.Problems1XX.P141_LinkedListCycle; | ||
|
||
public static class SlowFastPointerApproach | ||
{ | ||
public static bool HasCycle(ListNode? head) | ||
{ | ||
if (head == null) | ||
{ | ||
return false; | ||
} | ||
|
||
var slow = head; | ||
var fast = head; | ||
|
||
while (slow?.Next != null && fast?.Next != null) | ||
{ | ||
slow = slow.Next; | ||
fast = fast.Next.Next; | ||
|
||
if (slow == fast) | ||
{ | ||
return true; | ||
} | ||
} | ||
|
||
return false; | ||
} | ||
} |
100 changes: 100 additions & 0 deletions
100
...ode.Challenges.UnitTests/Problems1XX/P141_LinkedListCycle/SlowFastPointerApproachTests.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,100 @@ | ||
using LeetCode.Challenges.Problems1XX.P141_LinkedListCycle; | ||
using Shouldly; | ||
using Xunit; | ||
|
||
namespace LeetCode.Challenges.UnitTests.Problems1XX.P141_LinkedListCycle; | ||
|
||
public class SolutionTests | ||
{ | ||
[Theory] | ||
[MemberData(nameof(TestData))] | ||
public void GivenLinkedList_WhenCallHasCycle_ThenResultAsExpected(ListNode head, bool expectedResult) | ||
{ | ||
SlowFastPointerApproach.HasCycle(head).ShouldBe(expectedResult); | ||
} | ||
|
||
public static IEnumerable<object[]> TestData() | ||
{ | ||
yield return [null!, false]; | ||
yield return [new ListNode(1), false]; | ||
yield return [GetListWithCycle(), true]; | ||
yield return [GetListWithoutCycle(), false]; | ||
yield return [GetSingleNodeListWithCycle(), true]; | ||
yield return [GetListWithCycleAtTheBeginning(), true]; | ||
yield return [GetListWithCycleAtTheEnd(), true]; | ||
} | ||
|
||
private static ListNode GetListWithCycle() | ||
{ | ||
var second = new ListNode(2); | ||
var third = new ListNode(0); | ||
var fourth = new ListNode(-4); | ||
second.Next = third; | ||
third.Next = fourth; | ||
fourth.Next = second; | ||
var listWithCycle = new ListNode(3) { Next = second }; | ||
return listWithCycle; | ||
} | ||
|
||
private static ListNode GetListWithoutCycle() | ||
{ | ||
var listWithoutCycle = new ListNode(1) | ||
{ | ||
Next = new ListNode(2) | ||
{ | ||
Next = new ListNode(3) | ||
{ | ||
Next = new ListNode(4) | ||
{ | ||
Next = new ListNode(5) | ||
} | ||
} | ||
} | ||
}; | ||
|
||
return listWithoutCycle; | ||
} | ||
|
||
private static ListNode GetSingleNodeListWithCycle() | ||
{ | ||
var singleNodeCycle = new ListNode(1); | ||
singleNodeCycle.Next = singleNodeCycle; | ||
return singleNodeCycle; | ||
} | ||
|
||
private static ListNode GetListWithCycleAtTheBeginning() | ||
{ | ||
var list = new ListNode(1); | ||
var node2 = new ListNode(2); | ||
var node3 = new ListNode(3); | ||
var node4 = new ListNode(4); | ||
var node5 = new ListNode(5); | ||
|
||
list.Next = node2; | ||
node2.Next = node3; | ||
node3.Next = node4; | ||
node4.Next = node5; | ||
node5.Next = node2; | ||
|
||
return list; | ||
} | ||
|
||
private static ListNode GetListWithCycleAtTheEnd() | ||
{ | ||
var list = new ListNode(1); | ||
var secondNode = new ListNode(2); | ||
var thirdNode = new ListNode(3); | ||
var fourthNode = new ListNode(4); | ||
var fifthNode = new ListNode(5); | ||
var sixthNode = new ListNode(6); | ||
|
||
list.Next = secondNode; | ||
secondNode.Next = thirdNode; | ||
thirdNode.Next = fourthNode; | ||
fourthNode.Next = fifthNode; | ||
fifthNode.Next = sixthNode; | ||
sixthNode.Next = fourthNode; | ||
|
||
return list; | ||
} | ||
} |