Example #1
0
 //Bottom-Up Recursive Solution.
 //Line 1+2+5 => for return the tail of the list as new head.
 //Line 3+4 => for reverse the pointer while bottom-up.
 public ListNode ReverseLinkedList_Recursive_BottomUp(ListNode head)
 {
     if (head == null || head.next == null) return head;                 //Line 1: "head == null" is used to handle empty linkedlist. "head.next == null" is used to return the newHead(tail).
     ListNode tail = ReverseLinkedList_Recursive_BottomUp(head.next);    //Line 2: Recurse till the last node, and return it.
     head.next.next = head;      //Line 3: Change From [ prev -> head -> next ] to [ prev -> head <=> next ]
     head.next = null;           //Line 4: Change From [ prev -> head <=> next ] to [ prev -> head <- next ] + [ head -> null ]
     return tail;                                            //Line 5: work with Line 1 and 2 to pass the last node from deepest recursion to the top. Ensure the return value is the new head.
 }
Example #2
0
 //Delete the given node from the LinkedList (node cannot be tail node.)
 public void DeleteGivenNode(ListNode node)
 {
     if (node.next != null)
     {
         node.val = node.next.val;
         node.next = node.next.next;
     }
 }
Example #3
0
 //Delete all nodes that has the given value. Elegant Recursive solution.
 public ListNode DeleteGivenValue_Recursive(ListNode head, int value)
 {
     //Tail
     if (head == null) return null;
     //Delete node
     if (head.val == value) return DeleteGivenValue_Recursive(head.next, value);
     //Recurse all node.
     else head.next = DeleteGivenValue_Recursive(head.next, value);
     return head;
 }
Example #4
0
 public ListNode CreateLinkedList(int[] element)
 {
     ListNode prev = new ListNode(0);
     ListNode head = prev;
     for (int i = 0; i < element.Length; i++)
     {
         ListNode newNode = new ListNode(element[i]);
         prev.next = newNode;
         prev = prev.next;
     }
     return head.next;
 }
Example #5
0
        //Delete all nodes that has the given value. Iterative solution using dummy head.
        public ListNode DeleteGivenValue_Iterative(ListNode head, int value)
        {
            ListNode dummy = new ListNode(0);
            dummy.next = head;
            ListNode curr = dummy;

            while (curr.next != null)
            {
                if (curr.next.val == value) curr.next = curr.next.next;
                else curr = curr.next;
            }
            return dummy.next;
        }
Example #6
0
 //In each loop, Focus on reversing the "current node" (i.e. head) only.
 //Do not try to do too many things in one loop, because it confuse people.
 public ListNode ReverseLinkedList_Iterative(ListNode head)
 {
     //For the first node, prevNode is null
     //For the last node (null, loop exit condition), prevNode is the first node of the reversed linkedlist.
     ListNode prevNode = null;
     while (head != null)
     {
         ListNode nextNode = head.next;      //Before pointing "head.next" to the "prevNode", save the node "head.next" originally pointing at to "tail".
         head.next = prevNode;               //Reverse the current node by pointing "head.next" to the "prevNode".
         prevNode = head;                    //Current node completed. Move on to next node. Since the focus changed from head to next, prevNode should be changed to head.
         head = nextNode;                    //Since the focus changed from head to next, head should be changed to tail.
     }
     return prevNode;
 }
Example #7
0
        /// <summary>
        /// find the size of the stack using linked list.
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="head">The head.</param>
        /// <returns></returns>
        public static int SizeSLL <T>(ListNode <T> head)
        {
            int count = 0;

            if (head == null)
            {
                return(count);
            }
            else
            {
                ListNode <T> temp = head;

                while (temp != null)
                {
                    count++;
                    temp = temp.next;
                }
            }
            return(count);
        }
Example #8
0
        public static ListNode <T> Add <T>(ListNode <T> head, ListNode <T> newnode)
        {
            ListNode <T> temp;

            if (head == null)
            {
                head = newnode;
            }
            else
            {
                temp = head;

                while (temp.next != null)
                {
                    temp = temp.next;
                }

                temp.next = newnode;
            }
            return(head);
        }
Example #9
0
        public static ListNode <T> ArrayToOrderedLinkedList <T>(ListNode <T> head, T[] array)
        {
            ListNode <T> newnode = null;
            string       data;

            for (int i = 0; i < array.Length; i++)
            {
                data = (string)((object)array[i]);
                if (data != null)
                {
                    newnode      = new ListNode <T>();
                    newnode.data = (T)((object)data);
                    head         = AddInOrder(head, newnode);
                }
                else
                {
                    break;
                }
            }
            return(head);
        }
Example #10
0
        public static void LinkedListToFile <T>(ListNode <T> head, string path)
        {
            StreamWriter SW = new StreamWriter(path);

            if (head == null || !File.Exists(path))
            {
                Console.WriteLine("head is null or file doesnot exist");
            }
            else
            {
                ListNode <T> temp = head;

                while (temp != null)
                {
                    SW.Write(((string)((object)temp.data)), true);
                    SW.Write(" ", true);
                    temp = temp.next;
                }
            }

            SW.Close();
        }
Example #11
0
        /// <summary>
        /// calculate the size of linked list
        /// </summary>
        /// <param name="head"></param>
        /// <returns></returns>
        public static int ListSize <T>(ListNode <T> head)
        {
            int size = -1;

            if (head == null)
            {
                size = 1;
                return(size);
            }
            else
            {
                ListNode <T> temp = head;

                while (temp != null)
                {
                    size++;
                    temp = temp.next;
                }
            }

            return(size);
        }
Example #12
0
        /// <summary>
        /// Write the HashTable to a file
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="HashTable">The hash table.</param>
        /// <param name="path">The path.</param>
        public static void HashTableToFile <T>(ListNode <T>[] HashTable, string path)
        {
            StreamWriter SW = new StreamWriter(path);

            for (int i = 0; i < HashTable.Length; i++)
            {
                if (HashTable[i] != null || !File.Exists(path))
                {
                    ListNode <T> temp = HashTable[i];

                    while (temp != null)
                    {
                        string data = (string)((object)temp.data);
                        Console.WriteLine("data to be written : {0}", data);
                        SW.Write(data, true);
                        SW.Write("   ", true);
                        temp = temp.next;
                    }
                    SW.WriteLine(" ", true);
                }
            }
            SW.Close();
        }
Example #13
0
        /// <summary>
        /// if the item is found then return the index otherwise return -1;
        /// </summary>
        /// <param name="head"></param>
        /// <param name="item"></param>
        /// <returns></returns>
        public static int Index <T>(ListNode <T> head, ListNode <T> item)
        {
            int index = -1;

            if (head == null)
            {
                return(index);
            }
            else
            {
                index = 0;
                ListNode <T> temp = head;
                while (temp != null)
                {
                    if ((temp.data).Equals(item.data))
                    {
                        return(index);
                    }
                    index++;
                    temp = temp.next;
                }
            }
            return(index);
        }
Example #14
0
 public ListNode ReverseLinkedList_Recursive_TopDown(ListNode head)
 {
     return ReverseLinkedList(null, head);
 }
Example #15
0
 /// <summary>
 /// Determines whether the stack using the linked list is empty.
 /// </summary>
 /// <typeparam name="T"></typeparam>
 /// <param name="head">The head.</param>
 /// <returns>
 ///   <c>true</c> if [is empty stack ll] [the specified head]; otherwise, <c>false</c>.
 /// </returns>
 public static bool IsEmptyStackLL <T>(ListNode <T> head)
 {
     return(head == null);
 }
Example #16
0
 //Top-Down Recursive Solution.
 private ListNode ReverseLinkedList(ListNode prevNode, ListNode head)
 {
     if (head == null) return head;
     ListNode nextNode = head.next;      //IDENTICAL TO Iterative solution.
     head.next = prevNode;               //IDENTICAL TO Iterative solution.
     return ReverseLinkedList(head, nextNode);
 }
Example #17
0
        /// <summary>
        /// Creates the hash table using linked list.
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <returns></returns>
        public static ListNode <T>[] CreateHashTableUsingLinkedList <T>()
        {
            ListNode <T>[] HashTable = new ListNode <T> [11];

            return(HashTable);
        }
Example #18
0
        /// <summary>
        /// Creates the stack using linked list.
        /// </summary>
        /// <returns></returns>
        public static ListNode <T> CreateStackUsingLinkedList <T>()
        {
            ListNode <T> head = null;

            return(head);
        }
Example #19
0
 /// <summary>
 /// Pop the item form the index out of the stack using linked list.
 /// </summary>
 /// <typeparam name="T"></typeparam>
 /// <param name="head">The head.</param>
 /// <param name="index">The index.</param>
 /// <returns></returns>
 public static ListNode <T> Pop <T>(ref ListNode <T> head, int index)
 {
     ListNode <T>     temp;
     ListNode <T>     item;
     ref ListNode <T> refhead = ref head;