Exemple #1
0
 public void RemoveFirst()
 {
     if (head == null)
     {
         return;
     }
     else
     {
         head = head.Next;
     }
 }
Exemple #2
0
        public void Reverse()
        {
            var current = head;
            LinkedListNodePractice prev = null;

            while (current != null)
            {
                var temp = current.Next;
                current.Next = prev;
                prev         = current;
                current      = temp;
            }
            head = prev;
        }
Exemple #3
0
        public void AddFirst(int value)
        {
            var newNode = new LinkedListNodePractice(value);

            if (head == null)
            {
                head = newNode;
            }
            else
            {
                var oldHead = head;
                head         = newNode;
                newNode.Next = oldHead;
            }
        }
Exemple #4
0
        public void AddLast(int value)
        {
            var newNode = new LinkedListNodePractice(value);

            if (head == null)
            {
                head = newNode;
            }
            else
            {
                var current = head;
                while (current.Next != null)
                {
                    current = current.Next;
                }
                current.Next = newNode;
            }
        }
Exemple #5
0
 public void RemoveLast()
 {
     if (head == null)
     {
         return;
     }
     else
     {
         var current = head;
         LinkedListNodePractice prev = null;
         while (current.Next != null)
         {
             prev    = current;
             current = current.Next;
         }
         prev.Next = null;
     }
 }
Exemple #6
0
        public void RemoveAtIndex(int index)
        {
            int indexCount = 0;
            var current    = head;
            LinkedListNodePractice prev = null;

            while (current != null)
            {
                if (indexCount == index)
                {
                    //set temp to current
                    //prev.Next = temp.Next
                    prev.Next = current.Next;
                    return;
                }
                prev    = current;
                current = current.Next;
                indexCount++;
            }
        }
Exemple #7
0
        public void AddAtIndex(int value, int index)
        {
            var newNode    = new LinkedListNodePractice(value);
            int indexCount = 0;
            var current    = head;
            LinkedListNodePractice prev = null;

            while (current != null)
            {
                if (index == indexCount)
                {
                    //set temp to current
                    //set prev.next to newNode
                    //set newNode.next = temp
                    var temp = current;
                    prev.Next    = newNode;
                    newNode.Next = temp;
                    return;
                }
                prev    = current;
                current = current.Next;
                indexCount++;
            }
        }