using System.Collections.Generic;
public static void Main()
var a = new LinkedListNode(6);
var b = new LinkedListNode(5);
var c = new LinkedListNode(2);
var d = new LinkedListNode(1);
Console.WriteLine(ContainsCycle(a));
Console.WriteLine(ContainsCycle1(a));
Console.WriteLine("------------");
var a2 = new LinkedListNode(6);
var b2 = new LinkedListNode(5);
var c2 = new LinkedListNode(2);
var d2 = new LinkedListNode(1);
Console.WriteLine(ContainsCycle(a2));
Console.WriteLine(ContainsCycle1(a2));
public static bool ContainsCycle1(LinkedListNode root) {
while(fastRunner != null && fastRunner.Next != null) {
slowRunner = slowRunner.Next;
fastRunner = fastRunner.Next.Next;
if(fastRunner == slowRunner) {
public static bool ContainsCycle(LinkedListNode root) {
Dictionary<LinkedListNode, bool> checkedNodes = new Dictionary<LinkedListNode, bool>();
while (current != null) {
checkedNodes.TryGetValue(current, out contained);
checkedNodes.Add(current, true);
public static void PrintList(LinkedListNode root) {
Console.WriteLine(root.Value);
while(current.Next != null) {
Console.WriteLine(current.Value);
public static void Delete(LinkedListNode root) {
root.Value = root.Next.Value;
root.Next = root.Next.Next;
public class LinkedListNode {
public int Value { get; set; }
public LinkedListNode Next { get; set; }
public LinkedListNode (int value) {