コード例 #1
0
        public void RemoveDuplicates(CTCI_LLNode <T> head)
        {
            if (head == null)
            {
                throw new ArgumentNullException(nameof(head));
            }
            var pointer1 = head;

            while (pointer1 != null)
            {
                var pointer2 = pointer1;
                while (pointer2.Next != null)
                {
                    if (pointer2.Next.Value.Equals(pointer1.Value))
                    {
                        pointer2.Next = pointer2.Next.Next;
                    }
                    else
                    {
                        //prev = pointer2;
                        pointer2 = pointer2.Next;
                    }
                }
                pointer1 = pointer1.Next;
            }
        }
コード例 #2
0
        public void RemoveDuplicatesWithQueue(CTCI_LLNode <T> head)
        {
            if (head == null)
            {
                throw new ArgumentNullException(nameof(head));
            }
            var _hash = new HashSet <T>();
            //var _stack = new Stack<T>();
            //var _queue = new Queue<T>();
            CTCI_LLNode <T> previous = null;

            while (head != null)
            {
                if (!_hash.Contains(head.Value))
                {
                    _hash.Add(head.Value);
                    previous = head;
                }
                else
                {
                    previous.Next = head.Next;
                }
                head = head.Next;
            }
        }
コード例 #3
0
        public bool DeleteMiddle(CTCI_LLNode <T> node)
        {
            if (node == null || node.Next == null)
            {
                return(false);
            }
            CTCI_LLNode <T> next = node.Next;

            node.Value = next.Value;
            node.Next  = next.Next;
            return(true);
        }