Esempio n. 1
0
        /// <summary>
        /// http://www.geeksforgeeks.org/nth-node-from-the-end-of-a-linked-list/
        /// </summary>
        /// <param name="node"></param>
        /// <param name="n"></param>
        /// <returns></returns>
        public LinkedListNode FindMthNodeFromEnd(LinkedListNode head, int n)
        {
            LinkedListNode slow  = head;
            LinkedListNode fast  = head;
            int            count = 1;

            while (count < n)
            {
                if (fast == null)
                {
                    // n is greater than length of linked list.
                    return(null);
                }
                fast = fast.getNext();
                count++;
            }

            while (fast != null)
            {
                fast = fast.getNext();
                slow = slow.getNext();
            }

            return(slow);
        }
        public LinkedListNode DeleteNode(LinkedListNode node, int value)
        {
            if (node == null)
            {
                return(node);
            }
            if (node.getData() == value)
            {
                node = node.getNext();
            }
            LinkedListNode current;
            LinkedListNode prev = new LinkedListNode();

            current = node;

            while (current != null)
            {
                if (current.getData() == value)
                {
                    prev.setNext(current.getNext());
                }
                else
                {
                    prev = current;
                }
                current = current.getNext();
            }
            prev.setNext(null);
            return(prev);
        }
Esempio n. 3
0
        public LinkedListNode FindCenter(LinkedListNode node)
        {
            LinkedListNode n = null;

            n = node;
            LinkedListNode slow = node;
            LinkedListNode fast = node;

            while (node.getNext() != null)
            {
                slow = node;
                node = node.getNext();
                fast = node.getNext();
            }
            return(slow);
        }
Esempio n. 4
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="data"></param>
        public void AddLast(int data)
        {
            if (head == null)
            {
                head = new LinkedListNode();
                head.setData(data);
                head.setNext(null);
            }

            LinkedListNode toAdd = new LinkedListNode();

            toAdd = head;
            while (toAdd.getNext() != null)
            {
                toAdd = toAdd.getNext();
            }
            toAdd.setData(data);
        }
Esempio n. 5
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="data"></param>
        public void DeleteNode(int data)
        {
            if (head == null)
            {
                return;
            }

            LinkedListNode prev;
            LinkedListNode ll = new LinkedListNode();

            ll   = head;
            prev = new LinkedListNode();

            while (ll != null)
            {
                prev = ll;
                ll   = ll.getNext();
                if (ll.getData() == data)
                {
                    prev.setNext(ll.getNext());
                    break;
                }
            }
        }