Esempio n. 1
0
        public static Node append(Node head, int data)
        {
            if (head != null)
            {
                Node current = head;
                while (current.Next != null)
                {
                    current = current.Next;
                }

                current.Next = new Node();
                current.Next.Data = data;
                return head;
            }
            else
            {
                head = new Node();
                head.Data = data;
                return head;
            }
        }
Esempio n. 2
0
        public static Node AddToHead(Node head, int val)
        {
            //Inserts a new node in front of the existing one, returns new head.

            //Create new node.
            Node newFirstorHeadNode = new Node();
            newFirstorHeadNode.Data = val;

            if (head == null) // Empty list
            {
                //easy.  My linked list has one node, the one I just created.
                return newFirstorHeadNode;
            }

            //Otherwise
            //Point new one to old first one (which will now be second).
            newFirstorHeadNode.Next = head;

            //Return my new first item:
            return newFirstorHeadNode;
        }
Esempio n. 3
0
        public static void RecursivePrintList(Node head)
        {
            if (head == null)
            {
                Console.WriteLine();
                return;
            }

            Console.Write(head.Data + " ");

            //Head of the remainder of the list is just the next one after this one.
            RecursivePrintList(head.Next);
        }
Esempio n. 4
0
 public static Node SortList(Node head, int count)
 {
     // Basic Algorithm Steps
     //1. Find Min Node
     //2. Remove Min Node and attach it to new Sorted linked list
     //3. Repeat "count" number of times
     Node _current = head;
     Node _previous = _current;
     Node _min = _current;
     Node _minPrevious = _min;
     Node _sortedListHead = null;
     Node _sortedListTail = _sortedListHead;
     for (int i = 0; i < count; i++)
     {
         _current = head;
         _min = _current;
         _minPrevious = _min;
         //Find min Node
         while (_current != null)
         {
             if (_current.Data < _min.Data)
             {
                 _min = _current;
                 _minPrevious = _previous;
             }
             _previous = _current;
             _current = _current.Next;
         }
         // Remove min Node
         if (_min == head)
         {
             head = head.Next;
         }
         else if (_min.Next == null) //if tail is min node
         {
             _minPrevious.Next = null;
         }
         else
         {
             _minPrevious.Next = _minPrevious.Next.Next;
         }
         //Attach min Node to the new sorted linked list
         if (_sortedListHead != null)
         {
             _sortedListTail.Next = _min;
             _sortedListTail = _sortedListTail.Next;
         }
         else
         {
             _sortedListHead = _min;
             _sortedListTail = _sortedListHead;
         }
     }
     return _sortedListHead;
 }
Esempio n. 5
0
            public static void ReverseUsingRecursion(Node head)
            {
                if (head == null) return;

                if (head.Next == null)
                {
                    newHead = head;
                    return;
                }

                ReverseUsingRecursion(head.Next);
                head.Next.Next = head;
                head.Next = null;
            }
Esempio n. 6
0
            public static void Reverse(ref Node head)
            {
                if (head == null) return;

                Node prev = null, current = head, next = null;

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

                current.Next = prev;
                head = current;
            }
Esempio n. 7
0
            public static void PrintRecursive(Node head)
            {
                if (head == null)
                {
                    Console.WriteLine();
                    return;
                }

                Console.Write("{0} ", head.Data);
                PrintRecursive(head.Next);
            }
Esempio n. 8
0
            public static void Print(Node head)
            {
                if (head == null) return;

                Node current = head;
                do
                {
                    Console.Write("{0} ", current.Data);
                    current = current.Next;
                } while (current != null);
            }
Esempio n. 9
0
            public static void Append(ref Node head, int data)
            {
                c++;
                if (head != null)
                {
                    Node current = head;
                    while (current.Next != null)
                    {
                        current = current.Next;
                    }

                    current.Next = new Node();
                    current.Next.Data = data;
                }
                else
                {
                    head = new Node();
                    head.Data = data;
                }
            }