public Node FindLoopEnd(Node head) { if (head == null && head.next == null) { return(null); } Node slowPtr = head; Node fastPtr = head; while (fastPtr != null && fastPtr.next != null) { fastPtr = fastPtr.next.next; slowPtr = slowPtr.next; if (fastPtr == slowPtr) { break; } } slowPtr = head; LinkedListAdvanced linkedList = new LinkedListAdvanced(); while (fastPtr.next != slowPtr.next) { fastPtr = fastPtr.next; slowPtr = slowPtr.next; } return(fastPtr); }
static void Main(string[] args) { LinkedList linkedList = new LinkedList(); Node head = linkedList.CreateLinkedListOfTenNodes(); linkedList.Traverse(head); Console.WriteLine(); LinkedListAdvanced linkedListAdvanced = new LinkedListAdvanced(); int size = linkedList.Size(head); Console.WriteLine("Size of linked list recursively is: " + size); int sizeRecursive = linkedListAdvanced.GetSizeRecursively(head); Console.WriteLine("Size of linked list is: " + sizeRecursive); bool ifFound1 = linkedList.Search(20, head); Console.WriteLine("Element found: " + ifFound1); bool ifFound2 = linkedList.Search(110, head); Console.WriteLine("Element found: " + ifFound2); bool keyFound1 = linkedListAdvanced.SearchRecursively(20, head); Console.WriteLine("Element found: " + keyFound1); bool keyFound2 = linkedListAdvanced.SearchRecursively(110, head); Console.WriteLine("Element found: " + keyFound2); int middleElement1 = linkedListAdvanced.MiddleElementIterative(head); if (middleElement1 == 0) { Console.WriteLine("Linked List is Empty"); } else { Console.WriteLine("Middle Element is:" + middleElement1); } int middleElement2 = linkedListAdvanced.MiddleElementUsingPointers(head); if (middleElement2 == 0) { Console.WriteLine("Linked List is Empty"); } else { Console.WriteLine("Middle Element is:" + middleElement2); } int n = 6; int nthNode = linkedListAdvanced.FindNthNode(n, head); if (nthNode > 0) { Console.WriteLine(String.Format("Node at {0} position is {1}", n, nthNode)); } n = 4; int nthNodeFromEnd = linkedListAdvanced.FindNthNodeFromEnd(n, head); if (nthNodeFromEnd > 0) { Console.WriteLine(String.Format("Node at {0} position is {1}", n, nthNodeFromEnd)); } //Node head2 = linkedListAdvanced.Reverse (head); linkedList.Traverse(head); linkedListAdvanced.CreateLoop(4, head); bool hasloop = linkedListAdvanced.HasLoop(head); if (hasloop) { Console.WriteLine("LinkedList has loop."); } else { Console.WriteLine("LinkedList doesn't have loop."); } Console.WriteLine("Length of loop is : " + linkedListAdvanced.LoopLength(head)); Console.WriteLine("___"); Node loopStartNode = linkedListAdvanced.FindLoopStart(head); Console.WriteLine("First Node of loop is : " + loopStartNode.data); Node loopEndNode = linkedListAdvanced.FindLoopEnd(head); Console.WriteLine("Last Node of loop is : " + loopEndNode.data); linkedListAdvanced.RemoveLoop(head); linkedList.Traverse(head); Console.WriteLine(); // Node newHead =linkedListAdvanced.RotatebyKNode(4, head); // linkedList.Traverse(newHead); linkedListAdvanced.RemoveDuplicatesFromSortedLL(head); linkedList.Traverse(head); }