static void Main(string[] args)
        {
            //https://www.udemy.com/course/cpp-data-structures-algorithms-prateek-narang/learn/quiz/5245878#overview

            var numArray = new int[] { 1, 2, 3, 4, 5, 6, 7 };
            int k        = 3; //Find 3rd from last (5)


            LinkedList ll = LLHelper.CreateLinkedList(numArray);

            Node slow = ll.Head;
            Node fast = ll.Head;

            for (int i = 1; i <= k; i++)
            {
                fast = fast.Next;
            }
            while (fast != null)
            {
                slow = slow.Next;
                fast = fast.Next;
            }

            Console.WriteLine(slow.Value);
        }
Beispiel #2
0
        static void Main(string[] args)
        {
            Console.WriteLine("1. Reversing LL using auxiliary array");
            var numArray = new int[] { 5, 10, 13, 12, 18, 20, 25 };
            var ll       = LLHelper.CreateLinkedList(numArray);

            Console.WriteLine("Input LL");
            LLHelper.Display(ll.Head);
            Console.WriteLine();
            ll = ReverseUsingArray(ll);
            LLHelper.Display(ll.Head);
            Console.WriteLine();

            Console.WriteLine("2. Reversing LL using sliding pointers");
            ll = LLHelper.CreateLinkedList(numArray);
            Console.WriteLine("Input LL");
            LLHelper.Display(ll.Head);
            Console.WriteLine();
            ll = ReverseUsingSlidingPointers(ll);
            LLHelper.Display(ll.Head);
            Console.WriteLine();

            Console.WriteLine("3. Reversing LL using recursion");
            ll = LLHelper.CreateLinkedList(numArray);
            Console.WriteLine("Input LL");
            LLHelper.Display(ll.Head);
            Console.WriteLine();
            ll = ReverseUsingRecursion(ll);
            LLHelper.Display(ll.Head);
            Console.WriteLine();
        }
Beispiel #3
0
        static void Main(string[] args)
        {
            var numArray = new int[] { 1, 2, 3, 4, 5 };
            var dll      = LLHelper.CreateDoublyLinkedList(numArray);

            Console.WriteLine("Display Forward");
            LLHelper.DisplayDoublyForward(dll.Head);

            Console.WriteLine();

            Console.WriteLine("Display Backward");
            LLHelper.DisplayDoublyBackward(dll.Tail);

            Console.WriteLine();
            Console.WriteLine("Reversing Doubly LL");
            dll = Reverse(dll);

            Console.WriteLine("Display Forward");
            LLHelper.DisplayDoublyForward(dll.Head);

            Console.WriteLine();

            Console.WriteLine("Display Backward");
            LLHelper.DisplayDoublyBackward(dll.Tail);

            Console.WriteLine();
        }
        static void Main(string[] args)
        {
            var numArray = new int[] { 8, 6, 3, 9, 10, 4, 2, 12 };
            var ll       = LLHelper.CreateLinkedList(numArray);

            Console.WriteLine("Given LL1");
            LLHelper.Display(ll.Head);
            Console.WriteLine();

            var numArray1 = new int[] { 20, 30, 40, 10, 4, 2, 12 };
            var ll1       = LLHelper.CreateLinkedList(numArray1);

            Console.WriteLine("Given LL2");
            LLHelper.Display(ll1.Head);
            Console.WriteLine();

            //create intersection point

            var fifthNodeInLL1 = ll.Head.Next.Next.Next.Next;
            var thirdNodeInLL2 = ll1.Head.Next.Next;

            thirdNodeInLL2.Next = fifthNodeInLL1;
            /////////////////////////

            FindIntersectionPoint(ll, ll1);
        }
        static void Main(string[] args)
        {
            var numArray = new int[] { 5, 10, 13, 15, 18, 20, 25 };
            var ll       = LLHelper.CreateLinkedList(numArray);

            Console.WriteLine("The elements of linked list are : ");
            LLHelper.Display(ll.Head);

            Console.WriteLine();
            Console.WriteLine("Inserting number 2 ");
            int numberToInsert = 2;

            ll = InsertNum(ll, numberToInsert);
            LLHelper.Display(ll.Head);


            Console.WriteLine();
            Console.WriteLine("Inserting number 12 ");
            numberToInsert = 12;
            ll             = InsertNum(ll, numberToInsert);
            LLHelper.Display(ll.Head);


            Console.WriteLine();
            Console.WriteLine("Inserting number 30 ");
            numberToInsert = 30;
            ll             = InsertNum(ll, numberToInsert);
            LLHelper.Display(ll.Head);

            Console.WriteLine();
        }
        static void Main(string[] args)
        {
            var numArray = new int[] { 5, 10, 13, 12, 18, 20, 25 };
            var ll       = LLHelper.CreateLinkedList(numArray);

            Console.WriteLine("Input LL");
            LLHelper.Display(ll.Head);
            CheckSorted(ll);
        }
        static void Main(string[] args)
        {
            //https://www.udemy.com/course/cpp-data-structures-algorithms-prateek-narang/learn/quiz/5293152#overview
            var        numArray = new int[] { 5, 7, 17, 13, 11 };
            LinkedList ll1      = LLHelper.CreateLinkedList(numArray);

            var        numArray1 = new int[] { 12, 10, 2, 4, 6 };
            LinkedList ll2       = LLHelper.CreateLinkedList(numArray1);

            Node head = MergeLL(ll1, ll2);
        }
        static void Main(string[] args)
        {
            var numArray = new int[] { 2, 8, 10, 15, 18, 20 };
            var ll       = LLHelper.CreateLinkedList(numArray);

            Console.WriteLine("Given LL");
            LLHelper.Display(ll.Head);
            Console.WriteLine();

            FindMiddleNode(ll.Head);
        }
Beispiel #9
0
        static void Main(string[] args)
        {
            Console.Write("Enter size of linked list : ");
            int size     = int.Parse(Console.ReadLine());
            var numArray = new int[size];

            for (int i = 1; i <= size; i++)
            {
                Console.Write("Enter value for Node {0} : ", i);
                numArray[i - 1] = int.Parse(Console.ReadLine());
            }

            LinkedList ll = LLHelper.CreateLinkedList(numArray);

            Console.WriteLine("Display !");
            LLHelper.Display(ll.Head);


            Console.WriteLine();

            Console.WriteLine("Recursive Display !");
            RecursiveDisplay(ll.Head);
            Console.WriteLine();


            var sum = SumElements(ll);

            Console.WriteLine("Sum of elements is : {0}", sum);

            var maxValue = LargestNumber(ll);

            Console.WriteLine("Largest of elements is : {0}", maxValue);

            SearchAnElement(ll);

            ll = MoveSearchedNodeToHead(ll);

            Console.WriteLine("IMPORTANT >> : Searched element moved to HEAD, Reprinting MODIFIED LL");

            LLHelper.Display(ll.Head);
            Console.WriteLine();

            ll = InsertNodeAtBegining(ll);
            LLHelper.Display(ll.Head);
            Console.WriteLine();

            ll = InsertNodeAtNthPosition(ll);
            LLHelper.Display(ll.Head);
            Console.WriteLine();

            ll = InsertNodeAtNthPositionRecursive(ll);
            LLHelper.Display(ll.Head);
            Console.WriteLine();
        }
        static void Main(string[] args)
        {
            var numArray = new int[] { 5, 10, 13, 12, 18 };
            var ll       = LLHelper.CreateLinkedList(numArray);

            ll = CreateLoopKnowingly(ll);
            CheckLoop(ll);

            var numArray1 = new int[] { 5, 10, 13, 12, 18, 20, 25 };
            var ll1       = LLHelper.CreateLinkedList(numArray1);

            CheckLoop(ll1);
        }
        static void Main(string[] args)
        {
            var numArray = new int[] { 3, 5, 5, 8, 8, 8, 12 };
            var ll       = LLHelper.CreateLinkedList(numArray);

            Console.WriteLine("Input LL");
            LLHelper.Display(ll.Head);
            Console.WriteLine();
            Console.WriteLine("After duplicates removal");

            ll = RemoveDuplicates(ll);

            LLHelper.Display(ll.Head);
        }
        static void Main(string[] args)
        {
            var numArray  = new int[] { 1, 2, 3, 4, 5 };
            var dll       = LLHelper.CreateDoublyLinkedList(numArray);
            var termFirst = dll.Head;
            var termLast  = dll.Tail;

            termFirst.Previous = termLast;
            termLast.Next      = termFirst;

            Display(dll);

            Console.WriteLine("Inserting node 100 at first position");
            dll = InsertNodeAtFirstPosition(dll);

            Display(dll);
        }
Beispiel #13
0
        static void Main(string[] args)
        {
            var numArray = new int[] { 5, 10, 13, 15, 18, 20, 25 };
            var ll       = LLHelper.CreateLinkedList(numArray);

            Console.WriteLine("Initial LL");
            LLHelper.Display(ll.Head);
            ll = DeleteFirstNode(ll);
            Console.WriteLine();
            Console.WriteLine("LL after first node deletion");
            LLHelper.Display(ll.Head);
            Console.WriteLine();
            Console.WriteLine("LL after 4th node deletion");
            ll = DeleteNode(ll, 4);
            LLHelper.Display(ll.Head);
            Console.WriteLine();
        }
        static void Main(string[] args)
        {
            var numArray = new int[] { 5, 10, 13, 12, 18, 20, 25 };
            var ll1      = LLHelper.CreateLinkedList(numArray);

            Console.WriteLine("First LL");
            LLHelper.Display(ll1.Head);
            Console.WriteLine();

            var numArray1 = new int[] { 100, 130, 150 };
            var ll2       = LLHelper.CreateLinkedList(numArray1);

            Console.WriteLine("Second LL");
            LLHelper.Display(ll2.Head);
            Console.WriteLine();

            var llConcat = Concatenate(ll1, ll2);

            Console.WriteLine("Cocatenated LL");
            LLHelper.Display(llConcat.Head);
            Console.WriteLine();
        }
        static void Main(string[] args)
        {
            // Merge two sorted LL without using third LL
            var numArray1 = new int[] { 2, 8, 10, 15, 18 };
            var ll1       = LLHelper.CreateLinkedList(numArray1);

            Console.WriteLine("First LL");
            LLHelper.Display(ll1.Head);
            Console.WriteLine();

            var numArray2 = new int[] { 4, 7, 12, 14, 17, 19, 20 };
            var ll2       = LLHelper.CreateLinkedList(numArray2);

            Console.WriteLine("Second LL");
            LLHelper.Display(ll2.Head);
            Console.WriteLine();

            Console.WriteLine("Merged LL");
            var third = Merge(ll1, ll2);

            LLHelper.Display(third);
            Console.WriteLine();
        }
Beispiel #16
0
        static void Main(string[] args)
        {
            var numArray  = new int[] { 2, 8, 10, 15, 18 };
            var ll        = LLHelper.CreateLinkedList(numArray);
            var head      = ll.Head;
            var fifthTerm = head.Next.Next.Next.Next;

            fifthTerm.Next = head; // Made LL Circular

            Console.WriteLine("Display Iteratively");
            Display(ll);

            Console.WriteLine("Display Recursively");
            DisplayRecursively(ll.Head, ll.Head, true);
            Console.WriteLine();

            Console.WriteLine("Insert 100 before first node");
            ll = InsertNodeAtFirstPosition(ll);
            Display(ll);

            Console.WriteLine("Delete first node");
            ll = DeleteNodeAtFirstPosition(ll);
            Display(ll);
        }
        static void Main(string[] args)
        {
            var numArray = new int[] { 2, 10, 15, 18, 20, 25 };
            var dll      = LLHelper.CreateDoublyLinkedList(numArray);

            Console.WriteLine("Display Forward");
            LLHelper.DisplayDoublyForward(dll.Head);

            Console.WriteLine();

            Console.WriteLine("Display Backward");
            LLHelper.DisplayDoublyBackward(dll.Tail);

            Console.WriteLine();
            Console.WriteLine("Inserting 100 at first position");
            dll = InsertDoublyNodeAtFirstPosition(dll);

            Console.WriteLine("Display Forward");
            LLHelper.DisplayDoublyForward(dll.Head);

            Console.WriteLine();

            Console.WriteLine("Display Backward");
            LLHelper.DisplayDoublyBackward(dll.Tail);

            Console.WriteLine();

            Console.WriteLine("Insert 500 at third position");
            dll = InsertDoublyNodeAtNPosition(dll, 3);

            Console.WriteLine("Display Forward");
            LLHelper.DisplayDoublyForward(dll.Head);

            Console.WriteLine();

            Console.WriteLine("Display Backward");
            LLHelper.DisplayDoublyBackward(dll.Tail);

            Console.WriteLine();

            Console.WriteLine("Deleting node at first position");
            dll = DeleteDoublyNodeAtFirstPosition(dll);

            Console.WriteLine("Display Forward");
            LLHelper.DisplayDoublyForward(dll.Head);

            Console.WriteLine();

            Console.WriteLine("Display Backward");
            LLHelper.DisplayDoublyBackward(dll.Tail);

            Console.WriteLine();

            Console.WriteLine("Deleting node at third position");
            dll = DeleteDoublyNodeAtNPosition(dll, 3);

            Console.WriteLine("Display Forward");
            LLHelper.DisplayDoublyForward(dll.Head);

            Console.WriteLine();

            Console.WriteLine("Display Backward");
            LLHelper.DisplayDoublyBackward(dll.Tail);

            Console.WriteLine();
        }