Ejemplo n.º 1
0
        public void Print()
        {
            LinkedNode current = head;

            while (current != null)
            {
                Console.Write(current.data + "->");
                current = current.next;
            }
            Console.Write("Null\n");
        }
Ejemplo n.º 2
0
 public void AddNode(LinkedNode node)
 {
     if (head == null)
     {
         head = node;
     }
     else
     {
         var temp = head;
         while (temp.next != null)
         {
             temp = temp.next;
         }
         temp.next = node;
     }
 }
Ejemplo n.º 3
0
        /// <summary>
        /// Reverses the list.
        /// We ceate three variables, prev, current and next.
        /// Prev and next are set to null initially and current is set to the head.
        /// If current node is null, then head, previous and current are all null;
        /// If current node is not null, we swap prev and next of the current and move the current to next.
        /// When the current is null, we have reached the end of the list and we set the head to prev which at the time would be the last node before the current
        /// </summary>
        public void ReverseList()
        {
            LinkedNode prev    = null;
            LinkedNode current = head;
            LinkedNode next    = null;

            while (current != null)
            {
                next         = current.next;
                current.next = prev;
                prev         = current;
                current      = next;
            }
            //once we are done through the list, we set head to previous
            head = prev;
        }
Ejemplo n.º 4
0
 public LinkedNode(int d)
 {
     data = d;
     next = null;
 }