public static DataStructures.Node <T> GetNodeAtBeginningOfLoop <T>( DataStructures.LinkedList <T> linkedList) { if (linkedList.Head == null) { throw new ArgumentException("Cannot evaluate an empty list."); } DataStructures.Node <T> slowRunner = linkedList.Head; DataStructures.Node <T> fastRunner = linkedList.Head; while (true) { // Jump ahead one slowRunner = slowRunner.Next; if (fastRunner.Next != null) { // Jump ahead two fastRunner = fastRunner.Next.Next; } else { throw new ArgumentException("List does not have a loop."); } if (slowRunner == fastRunner) { return(slowRunner); } } }
public static Node <T> GetNthToLastElement(DataStructures.LinkedList <T> linkedList, int n) { Node <T> forwardIterator = linkedList.Head; Node <T> backIterator = linkedList.Head; // Advance n times to create an n-sized gap between the iterators for (int i = 0; i < n; i++) { forwardIterator = forwardIterator.Next; // We hit the end of the list - there aren't enough elements to return the // nth from the last element if (forwardIterator == null) { throw new ArgumentException( String.Format("End of list encountered - cannot return the " + "element at index {0} - list does not have enough elements.", n) ); } } while (forwardIterator != null) { forwardIterator = forwardIterator.Next; backIterator = backIterator.Next; } return(backIterator); }
public static DataStructures.LinkedList <T> MergeSortedLists <T>(Node <T>[] nodes) where T : IComparable <T> { var result = new DataStructures.LinkedList <T>(); while (nodes.Any(x => x != null)) { //Find min element and index //I believe we can't use Linq for this because we need the reference in the array Node <T> min = null; int minIndex = 0; for (int i = 0; i < nodes.Length; ++i) { if (nodes[i] == null) { continue; } if (min == null) { min = nodes[i]; minIndex = i; } else if (nodes[i].Data.CompareTo(min.Data) < 0) { min = nodes[i]; minIndex = i; } } result.Add(min.Data); nodes[minIndex] = nodes[minIndex].Next; } return(result); }
public static void RemoveDuplicatesNoBuffer(DataStructures.LinkedList <T> linkedList) { if (linkedList.Head == null) { return; } Node <T> backIterator = linkedList.Head; while (backIterator != null) { Node <T> forwardIterator = backIterator.Next; Node <T> previousNode = backIterator; while (forwardIterator != null) { // If we have found a dupe, remove it from the list. if (forwardIterator.Value.Equals(backIterator.Value)) { previousNode.Next = forwardIterator.Next; } previousNode = previousNode.Next; forwardIterator = forwardIterator.Next; } backIterator = backIterator.Next; } }
public static void RemoveDuplicates(DataStructures.LinkedList <T> linkedList) { if (linkedList.Head == null) { return; } var seen = new Dictionary <T, bool>(); seen.Add(linkedList.Head.Value, true); Node <T> iterator = linkedList.Head; while (iterator.Next != null) { Node <T> previous = iterator; iterator = iterator.Next; if (seen.ContainsKey(iterator.Value)) { previous.Next = iterator.Next; } else { seen.Add(iterator.Value, true); } } }
public static Node <T> DetectLoop <T>(this DataStructures.LinkedList <T> list) { Node <T> n = list.Head; Dictionary <Node <T>, Node <T> > dict = new Dictionary <Node <T>, Node <T> >(); if (n == null) { return(null); } dict.Add(n, n); while (n.Next != null && dict.TryAdd(n.Next, n.Next)) { n = n.Next; } return(n); }
public static int RemoveDuplicates <T>(this DataStructures.LinkedList <T> list) { Node <T> n = list.Head; Dictionary <T, T> dict = new Dictionary <T, T>(); int removedCount = 0; while (n != null) { if (!dict.TryAdd(n.Data, n.Data)) { n = n.Prev; list.RemoveNode(n.Next); removedCount++; } n = n.Next; } return(removedCount); }
static void Main( ) { DataStructures.LinkedList <int> intList = new DataStructures.LinkedList <int>( ); // View the linked list of integers as a collection. ICollection <int> c = intList; Write("New c:"); foreach (int i in c) { Write(" {0}", i); } WriteLine( ); // Test collection 'Add' method. c.Add(3); c.Add(5); c.Add(1); c.Add(4); Write("After 'Add' c:"); foreach (int i in c) { Write(" {0}", i); } WriteLine( ); // Test collection 'Clear' method. c.Clear( ); Write("After 'Clear' c:"); foreach (int i in c) { Write(" {0}", i); } WriteLine( ); // Test collection 'Contains' method. c.Add(3); c.Add(5); c.Add(1); c.Add(4); Write("After 'Add' c:"); foreach (int i in c) { Write(" {0}", i); } WriteLine( ); WriteLine("c.Contains( 5 ) = {0}", c.Contains(5)); WriteLine("c.Contains( 6 ) = {0}", c.Contains(6)); // Test collection 'CopyTo' method. int[] a = new int[8]; c.CopyTo(a, 3); Write("After 'CopyTo' a:"); foreach (int i in a) { Write(" {0}", i); } WriteLine( ); // Test collection 'Remove' method. WriteLine("c.Remove( 5 ) = {0}", c.Remove(5)); Write("After 'c.Remove( 5 )' c:"); foreach (int i in c) { Write(" {0}", i); } WriteLine( ); WriteLine("c.Remove( 6 ) = {0}", c.Remove(6)); Write("After 'c.Remove( 6 )' c:"); foreach (int i in c) { Write(" {0}", i); } WriteLine( ); // Text collection 'Count' property. WriteLine("c.Count = {0}", c.Count); // Text collection 'IsReadOnly' property. WriteLine("c.IsReadOnly = {0}", c.IsReadOnly); }
public static DataStructures.LinkedList <int> AddLinkedLists( DataStructures.LinkedList <int> first, DataStructures.LinkedList <int> second) { if (first.Head == null && second.Head == null) { return(null); } var summedLists = new DataStructures.LinkedList <int>(); var summedRunner = new DataStructures.Node <int>(0); summedLists.Head = summedRunner; DataStructures.Node <int> firstRunner = first.Head; DataStructures.Node <int> secondRunner = second.Head; bool carry = false; while (firstRunner != null || secondRunner != null) { var newNode = new DataStructures.Node <int>(0); int sum = 0; if (carry) { sum++; } if (firstRunner == null) { sum += secondRunner.Value; } else if (secondRunner == null) { sum += firstRunner.Value; } else { sum += firstRunner.Value + secondRunner.Value; } carry = false; if (sum >= 10) { sum -= 10; carry = true; } summedRunner.Value = sum; summedRunner.Next = newNode; summedRunner = summedRunner.Next; firstRunner = firstRunner == null ? null : firstRunner.Next ?? null; secondRunner = secondRunner == null ? null : secondRunner.Next ?? null; } return(summedLists); }