public static void RunExercise() { Console.WriteLine("Exercise 2: Return Kth to last"); var elements = new int[] { 1, 2, 3, 4, 5, 6, 7, 8 }; var list = new LinkedList <int>(elements); Console.WriteLine("\n Original Linked List: "); Console.Write(" "); LinkedListPrinter.PrintLinkedList <int>(list); Console.WriteLine("\n Kth to last elements:"); var zerothElement = FindKthToLast(list, 0); Console.WriteLine(" 0th to last element: {0}", zerothElement); var firstElement = FindKthToLast(list, 1); Console.WriteLine(" 1st to last element: {0}", firstElement); var secondElement = FindKthToLast(list, 2); Console.WriteLine(" 2nd to last element: {0}", secondElement); var thirdElement = FindKthToLast(list, 3); Console.WriteLine(" 3rd to last element: {0}", thirdElement); Console.WriteLine(".\n.\n.\n"); }
public static void RunExercise() { Console.WriteLine("Exercise 3: Delete middle node"); var list = new LinkedList <int>(new int[] { 1, 2, 3, 4, 5, 6, 7, 8 }); Console.WriteLine("\n Original Linked List: "); Console.Write(" "); LinkedListPrinter.PrintLinkedList <int>(list); var listWithoutElementToRemove = RemoveElement(list, 4); Console.WriteLine("\n Linked List after removing the element '4': "); Console.Write(" "); LinkedListPrinter.PrintLinkedList <int>(listWithoutElementToRemove); }
public static void RunExercise() { Console.WriteLine("Exercise 1: Remove duplicates from a Linked List"); var elements = new int[] { 1, 2, 3, 4, 4, 5, 5, 6, 7, 8, 8 }; var list = new LinkedList <int>(elements); Console.WriteLine("\n Original Linked List: "); Console.Write(" "); LinkedListPrinter.PrintLinkedList <int>(list); var linkedListWithoutDups = RemoveDuplicates(list); Console.WriteLine("\n Linked List after removing duplicates: "); Console.Write(" "); LinkedListPrinter.PrintLinkedList <int>(linkedListWithoutDups); Console.WriteLine(); }