Skip to content

Commit

Permalink
Solve LeetCode #141 'Linked List Cycle' [Easy].
Browse files Browse the repository at this point in the history
  • Loading branch information
eminencegrs committed Nov 9, 2024
1 parent 31a9689 commit 9654bb8
Show file tree
Hide file tree
Showing 4 changed files with 143 additions and 0 deletions.
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`.
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;
}
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;
}
}
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;
}
}

0 comments on commit 9654bb8

Please sign in to comment.