public ListNode(int value) { Value = value; }
static bool HasCycle(ListNode head)
while (runner.Next != null && runner.Next.Next != null)
runner = runner.Next.Next;
if (walker == runner) return true;
static ListNode DetectCycle(ListNode head)
while (runner.Next != null && runner.Next.Next != null)
runner = runner.Next.Next;
ListNode tempWalker = head;
while (tempWalker != walker)
tempWalker = tempWalker.Next;
public static void Main()
ListNode node1 = new ListNode(1);
ListNode node2 = new ListNode(2);
ListNode node3 = new ListNode(3);
ListNode node4 = new ListNode(4);
bool hasCycle = HasCycle(node1);
Console.WriteLine(DetectCycle(node1).Value);