/// <summary>
        /// Time Complexity is O(n^2). Space Complexity is 1
        /// </summary>
        /// <returns></returns>
        public object BrutalForceMethod(SingleLinkedList list, int n)
        {
            if (list.Head == null)
            {
                return null;
            }
            LinkedListNode current = list.Head;
            LinkedListNode temp = current;
            int remainingNodes = 0;
            while (temp.Next != null)
            {
                remainingNodes++;
                if (remainingNodes >= n)
                {
                    current = current.Next;
                    temp = current;
                    remainingNodes = 0;
                }
                else
                {
                    temp = temp.Next;
                }
            }

            if (remainingNodes == n - 1)
            {
                return current;
            }
            else
            {
                return null;
            }
        }
        /// <summary>
        /// Time Complexity is O(n). Space Complexity is O(n)
        /// </summary>
        /// <returns></returns>
        public object UsingLength(SingleLinkedList list, int n)
        {
            if (list.Head == null)
            {
                return null;
            }
            var current = list.Head;
            int l = 1;
            //calculate length
            while (current.Next != null)
            {
                l++;
                current = current.Next;
            }

            int i = l - n + 1;
            if (i > 0)
            {
                current = list.Head;
                while (i > 1)
                {
                    current = current.Next;
                    i--;
                }
                return current;
            }
            return null;
        }
Example #3
0
 public void Intialize()
 {
     list = new SingleLinkedList();
     list.AddFront(3);
     list.AddFront(2);
     list.AddFront(1);
     list.Print();
 }
Example #4
0
 public void TestReverseRecursive()
 {
     Console.WriteLine("Test started!");
     ReverseList reverse = new ReverseList();
     list = reverse.ReverseLinkedListRecursive(list);
     Console.WriteLine("Reverse Ended!");
     list.Print();
     Console.WriteLine("Test Ended!");
 }
 public void Intialize()
 {
     EmptyList = new SingleLinkedList();
     SingleNodeList = new SingleLinkedList();
     SingleNodeList.AddLast(1);
     List = new SingleLinkedList();
     List.AddLast(1);
     List.AddLast(2);
     List.AddLast(3);
     List.AddLast(4);
     List.AddLast(5);
     List.AddLast(6);
 }
Example #6
0
    public SingleLinkedList ReverseLinkedListRecursive(SingleLinkedList list)
    {
        if(list.Head == null) return list;

        var head = list.Head;
        var tail = list.Tail;
        ReverseNodes(list.Head);

        list.Tail = head;
        list.Head = tail;

        return list;
    }
Example #7
0
    public SingleLinkedList ReverseLinkedListIterative(SingleLinkedList list)
    {
        Console.WriteLine("Reverse Started!");
        if(list.Head == null) return null;

        LinkedListNode current = list.Head;
        LinkedListNode previous = null;
        LinkedListNode next = current.Next;

        while(current != null)
        {
            next = current.Next;
            current.Next = previous;
            previous = current;
            current = next;
        }

        list.Head = previous;
        return list;
    }
        public object UsingHashTable(SingleLinkedList list, int n)
        {
            if (list.Head == null)
            {
                return null;
            }
            Hashtable table = new Hashtable();
            var current = list.Head;

            for (int i = 1; current != null; i++)
            {
                table.Add(i,current);
                current = current.Next;
            }

            int x = table.Count - n + 1;
            if (x > 0)
            {
                return table[x];
            }
            return null;
        }
        public object UsingRangeMethod(SingleLinkedList list, int n)
        {
            if (list.Head == null)
            {
                return null;
            }

            var first = list.Head;
            var last = list.Head;

            for (int i = 1; i < n; i++)
            {
                if (last.Next != null)
                {
                    last = last.Next;
                }
                else
                {
                    return null;
                }
            }

            while (last.Next != null)
            {
                last = last.Next;
                first = first.Next;
            }

            return first;
        }
 public SingleLinkedListTests()
 {
     List = new SingleLinkedList();
 }