private Node FindALoop(CLinkedList cLinkedList) { var root = cLinkedList.GetRoot(); HashSet <int> hs = new HashSet <int>(); if (root != null) { var pointer = root; while (pointer.next != null) { if (found(ref hs, pointer.Data)) { return(pointer); } pointer = pointer.next; } if (found(ref hs, pointer.Data)) { return(pointer); } } return(cLinkedList.GetRoot()); }
private int LinkedListToNumber(CLinkedList c) { var start = c.GetRoot(); int dig = 0; int number = 0; while (true) { if (start.Data != 0) { int mutl = (int)Math.Pow(10, dig); number += (start.Data * mutl); } else { number += 0; } dig++; if (start.next == null) { break; } start = start.next; } return(number); }
public void FindDuplicate() { CLinkedList c = new CLinkedList(); c.Insert(4); c.Insert(3); c.Insert(1); c.Insert(2); c.Insert(4); c.print(); var node = FindALoop(c); if (node != c.GetRoot()) { Console.WriteLine($"Faulty node is {node.Data}"); } else { Console.WriteLine($"No faulty node in c"); } CLinkedList c2 = new CLinkedList(); c2.Insert(5); c2.Insert(1); c2.Insert(3); c2.Insert(4); c2.print(); var node2 = FindALoop(c2); if (node2 != c2.GetRoot()) { Console.WriteLine($"Faulty node is {node2.Data}"); } else { Console.WriteLine($"No faulty node in c2"); } }