//Getting length of Loop int FindLoopLength(ISinglyCell <int> head) { ISinglyCell <int> slowPointer = head; ISinglyCell <int> fastPointer = head; bool isCycleExists = false; while (slowPointer != null && fastPointer != null && fastPointer.Next != null) { slowPointer = slowPointer.Next; fastPointer = fastPointer.Next.Next; if (slowPointer == fastPointer) { isCycleExists = true; break; } } if (!isCycleExists) { return(0); } int counter = 0; // Reseting slowPointer fastPointer = fastPointer.Next; // Further slow and fast Pointers will move the same speed. while (slowPointer != fastPointer) { counter++; fastPointer = fastPointer.Next; } return(counter); }
// Getting FirstElement of Loop private ISinglyCell <int> FingBeginOfLoop(ISinglyCell <int> head) { ISinglyCell <int> slowPointer = head; ISinglyCell <int> fastPointer = head; bool isCycleExists = false; while (slowPointer != null && fastPointer != null && fastPointer.Next != null) { slowPointer = slowPointer.Next; fastPointer = fastPointer.Next.Next; if (slowPointer == fastPointer) { isCycleExists = true; break; } } if (!isCycleExists) { return(null); } // Reseting slowPointer slowPointer = head; // Further slow and fast Pointers will move the same speed. while (slowPointer != fastPointer) { fastPointer = fastPointer.Next; slowPointer = slowPointer.Next; } return(slowPointer); }
public List <string> Go() { ISinglyCell <int> head = CreatingLinkedList.Create(new SinglyCell <int>(), 5, false); ISinglyCell <int> middle = FindTheMiddle(head); return(new List <string>()); }
public List <bool> Go() { ISinglyCell <int> cell1 = null; ISinglyCell <int> next = null; int i = 0; while (i < 4) { if (cell1 == null) { cell1 = new SinglyCell <int>(); cell1.Value = 1; i++; next = cell1; continue; } ISinglyCell <int> cell = new SinglyCell <int>(); cell.Value = 1; next.Next = cell; next = next.Next; i++; } ISinglyCell <int> outer = cell1; while (outer.Next != null) { if (outer.Value == outer.Next.Value) { outer.Next = outer.Next.Next; } } return(new List <bool>()); }
public List <string> DisplayResult <T>(List <ISinglyCell <T> > list, bool hasSentinel = false) { if (list.Count == 0) { return(new List <string> { "There is no data for displaying" }); } List <string> resList = new List <string>(); if (hasSentinel) { ISinglyCell <T> cell = list.First(); while (cell.Next != null) { resList.Add("Value: " + cell.Value + ", Next Value: " + (cell.Next == null?"null":(cell.Next.Value == null?"null": cell.Next.Value.ToString())) + "\n"); cell = cell.Next; } } else { // list.Insert(0, new SinglyCell<T>()); ISinglyCell <T> cell = list.First(); while (cell != null) { resList.Add("Value: " + cell.Value + ", Next Value: " + (cell.Next == null ? "null" : (cell.Next.Value == null ? "null" : cell.Next.Value.ToString())) + "\n"); cell = cell.Next; } } return(resList); }
private ISinglyCell <int> GetNthNodeFromTheEnd(ISinglyCell <int> head, int NthNode) { ISinglyCell <int> tCell = head; ISinglyCell <int> pNthNodeCell = null; // Move first pointer from the head to NthNode node for (int count = 1; count < NthNode; count++) { if (tCell != null) { tCell = tCell.Next; } } // tCell- node is 4 point far away from the begining // pNthNodeCell - node starting from the begining while (tCell != null) { if (pNthNodeCell == null) { pNthNodeCell = head; } else { // They are both moves simultaneously // And after tCell reaches last cell, pNthNodeCell will contain node that is located from the End at N pNthNodeCell = pNthNodeCell.Next; tCell = tCell.Next; } } if (pNthNodeCell == null) { return(null); } return(pNthNodeCell); }
// O(N) private ISinglyCell <int> InsertNodeAtPosition(ISinglyCell <int> head, int position, int value) { SinglyCell <int> newNode = new SinglyCell <int> { Value = value }; if (head == null) { head = newNode; } if (position == 1) { newNode.Next = head; return(newNode); } int k = 1; ISinglyCell <int> prevNode = head; ISinglyCell <int> nextNode = head; while (nextNode != null && k < position) { k++; prevNode = nextNode; nextNode = nextNode.Next; } prevNode.Next = newNode; newNode.Next = nextNode; return(head); }
public List <string> Sort() { List <string> resList = new List <string>(); ISinglyCell <int> cell = firstOrderedLinkedList.First(); ISinglyCell <int> sentinel = new SinglyCell <int>(); sentinel.Next = null; while (cell != null) { // Get the next cell to add to the list. ISinglyCell <int> nextCell = cell; // Move input to input.Next for the next trip through the loop. cell = cell.Next; // See where to add the next item in the sorted list. ISinglyCell <int> afterMe = sentinel; while (afterMe.Next != null && afterMe.Next.Value < nextCell.Value) { afterMe = afterMe.Next; } // Insert the item in the sorted list. nextCell.Next = afterMe.Next; afterMe.Next = nextCell; } return(resList); }
public List <string> Go() { ISinglyCell <int> cell = CreatingLinkedList.Create(new SinglyCell <int>(), 10, false); Print(cell); return(new List <string>()); }
public List <bool> Go() { ISinglyCell <int> cell = CreatingLinkedList.Create(new SinglyCell <int>(), 10, false); var f = Reverse(cell); return(new List <bool>()); }
// O(N) private ISinglyCell <int> DeleteNode(ISinglyCell <int> head, int position) { if (head == null) { return(head); } if (position == 1) { return(head.Next); } ISinglyCell <int> previouCell = head; ISinglyCell <int> nextCell = head; int k = 1; while (nextCell != null && k < position) { k++; previouCell = nextCell; nextCell = nextCell.Next; } if (nextCell == null) { return(head); } previouCell.Next = nextCell.Next; return(head); }
public List <string> Go() { ISinglyCell <int> list1 = CreatingLinkedList.Create(new SinglyCell <int>(), 4, true); ISinglyCell <int> list2 = CreatingLinkedList.Create(new SinglyCell <int>(), 3, true); var res = RecursiveMerging(list2, list1); return(new List <string>()); }
private void Print(ISinglyCell <int> head) { if (head == null) { return; } Print(head.Next); Console.Write(head.Value); }
public List <string> Go() { ISinglyCell <int> cell = CreatingLinkedList.Create(new SinglyCell <int>(), 10, true); ISinglyCell <int> givenNode = new SinglyCell <int>(); givenNode.Value = 5465; ISinglyCell <int> res = InsertIntoSortingLinkedList(cell, givenNode); return(new List <string>()); }
private bool IsLinkedListEvenGo(ISinglyCell <int> head) { ISinglyCell <int> cell = head; while (cell != null && cell.Next != null) { cell = cell.Next.Next; } return(cell == null); }
ISinglyCell <int> GetLatestCell(ISinglyCell <int> head) { ISinglyCell <int> cell = head; ISinglyCell <int> prevCell = null; while (cell != null) { prevCell = cell; cell = cell.Next; } return(prevCell); }
private ISinglyCell <int> FindTheMiddle(ISinglyCell <int> cell) { ISinglyCell <int> slowPointer = cell; ISinglyCell <int> fastPointer = cell; int i = 0; while (fastPointer != null && fastPointer.Next != null) { fastPointer = fastPointer.Next.Next; slowPointer = slowPointer.Next; } return(slowPointer); }
private ISinglyCell <int> FindIntersectingNode(ISinglyCell <int> list1, ISinglyCell <int> list2) { // Determine length of list1 int l1 = 0; int l2 = 0; int dif = 0; ISinglyCell <int> head1 = list1; while (head1 != null) { l1++; head1 = head1.Next; } ISinglyCell <int> head2 = list2; while (head2 != null) { l2++; head2 = head2.Next; } if (l1 >= l2) { head1 = list1; head2 = list2; dif = l1 - l2; } else { head1 = list2; head2 = list1; dif = l2 - l1; } for (int i = 0; i < dif; i++) { head1 = head1.Next; } while (head1 != null && head2 != null) { if (head1 == head2) { return(head2); } head1 = head1.Next; head2 = head2.Next; } return(null); }
private ISinglyCell <int> Reverse(ISinglyCell <int> head) { ISinglyCell <int> prev = null; while (head != null) { ISinglyCell <int> next = head.Next; head.Next = prev; prev = head; head = next; } // !!!! Don't forget we have to return prev instead of head return(prev); }
//Determine existing private bool DoesLLhasALoop(ISinglyCell <int> head) { ISinglyCell <int> slPointer = head; ISinglyCell <int> fastPointer = head; // The Hare and Tortoise principle. This approach was found out by Floyd. while (slPointer != null && fastPointer != null && fastPointer.Next != null) { slPointer = slPointer.Next; // slow pointer will point to the next fastPointer = fastPointer.Next.Next; // slow pointer will point over one point if (slPointer == fastPointer) { return(true); } } return(false); }
public List <string> Go() { // Create general tail for two linked lists ISinglyCell <int> intermediate = CreatingLinkedList.Create(new SinglyCell <int>(), 4, false); // Creat first linked list ISinglyCell <int> list1 = CreatingLinkedList.Create(new SinglyCell <int>(), 3, false); //set tail GetLatestCell(list1).Next = intermediate; // Create second Linked List ISinglyCell <int> list2 = CreatingLinkedList.Create(new SinglyCell <int>(), 4, false); // Set tail GetLatestCell(list2).Next = intermediate; //Find InterSection Node var res = FindIntersectingNode(list1, list2); return(new List <string>()); }
// O(N) private ISinglyCell <int> InsertIntoSortingLinkedList(ISinglyCell <int> head, ISinglyCell <int> givenNode) { if (head == null) { return(head); } ISinglyCell <int> nextNode = head; ISinglyCell <int> previousNode = head; // Keep track previous Node and next Node. while (nextNode != null && nextNode.Value < givenNode.Value) { previousNode = nextNode; nextNode = nextNode.Next; } // Given Node will be inserted between those values. previousNode.Next = givenNode; givenNode.Next = nextNode; return(head); }
private ISinglyCell <int> CreateLinkedListFromBST(IBinaryNode <int> root, ISinglyCell <int> linkedNode) { var currentNode = linkedNode; if (root.LeftNode != null) { currentNode = CreateLinkedListFromBST(root.LeftNode, currentNode); } var node = new SinglyCell <int>(); node.Value = root.Value; currentNode.Next = node; currentNode = node; if (root.RightNode != null) { CreateLinkedListFromBST(root.RightNode, currentNode); } return(currentNode); }
public List <string> Go() { ISinglyCell <int> head = CreatingLinkedList.Create(new SinglyCell <int>(), 10, false); ISinglyCell <int> cell = head; ISinglyCell <int> givenCell = null; ISinglyCell <int> previousCell = null; int k = 1; // To create loop the end.Next will point to the k-th node from the begining. while (cell != null) { if (k == 4) { givenCell = cell; } previousCell = cell; cell = cell.Next; k++; } previousCell.Next = givenCell; var res = DoesLLhasALoop(head); return(new List <string>()); }
public static ISinglyCell <int> Create(ISinglyCell <int> head, int length, bool needToBeSorted) { Random r = new Random(); int[] arr = new int[length]; arr = arr.Select(x => r.Next(10000)).ToArray(); if (needToBeSorted) { Array.Sort(arr); } ISinglyCell <int> next = head; for (int i = 0; i < arr.Length; i++) { next.Value = arr[i]; if (i != arr.Length - 1) { next.Next = new SinglyCell <int>(); next = next.Next; } } return(head); }
private ISinglyCell <int> RecursiveMerging(ISinglyCell <int> list1, ISinglyCell <int> list2) { ISinglyCell <int> result = null; if (list1 == null) { return(list2); } if (list2 == null) { return(list1); } if (list1.Value <= list2.Value) { result = list1; result.Next = RecursiveMerging(list1.Next, list2); } else { result = list2; result.Next = RecursiveMerging(list2.Next, list1); } return(result); }
public List <int> Go() { ISinglyCell <int> head = CreatingLinkedList.Create(new SinglyCell <int>(), 10, false); return(new List <int>()); }
public InsertingLinkedListNodeByGivenPosition() { head = CreatingLinkedList.Create(new SinglyCell <int>(), 10, false); }