示例#1
0
        public ComparableNode RemoveWithRunner(ComparableNode node)
        {
            var slow = node;
            var fast = node;

            while (slow != null)
            {
                var previous = slow;

                while (fast != null)
                {
                    if (slow.CompareTo(fast) == 0)
                    {
                        previous.Next = fast.Next;
                    }
                    else
                    {
                        previous = fast;
                    }

                    fast = fast.Next;
                }

                slow = slow.Next;

                if (slow != null)
                {
                    fast = fast.Next;
                }
            }

            return(node);
        }
示例#2
0
        public ComparableNode Remove(ComparableNode node)
        {
            var existingNodes = new Dictionary <int, bool>();

            var head     = node;
            var previous = node;

            while (node != null)
            {
                if (existingNodes.ContainsKey(node.Value))
                {
                    previous.Next = node.Next;
                }
                else
                {
                    existingNodes.Add(node.Value, true);
                }

                previous = node;
                node     = node.Next;
            }

            return(head);
        }
示例#3
0
        public ComparableNode Partition(ComparableNode node, ComparableNode pivotNode)
        {
            if (node == null)
            {
                return(null);
            }

            ComparableNode leftBegin  = null;
            ComparableNode leftEnd    = null;
            ComparableNode rightBegin = null;
            ComparableNode rightEnd   = null;

            while (node != null)
            {
                if (node.CompareTo(pivotNode) == 0)
                {
                    if (leftBegin == null)
                    {
                        leftBegin = node;
                        leftEnd   = leftBegin;
                    }
                    else
                    {
                        leftEnd.Next = node;
                        leftEnd      = leftEnd.Next;
                    }
                }
                else
                {
                    if (rightBegin == null)
                    {
                        rightBegin = node;
                        rightEnd   = rightBegin;
                    }
                    else
                    {
                        rightEnd.Next = node;
                        rightEnd      = rightEnd.Next;
                    }
                }

                node = node.Next;
            }

            if (leftEnd != null)
            {
                leftEnd.Next = null;
            }

            if (rightEnd != null)
            {
                rightEnd.Next = null;
            }

            if (leftBegin == null && rightBegin != null)
            {
                return(rightBegin);
            }

            if (rightBegin == null)
            {
                return(leftBegin);
            }

            leftEnd.Next = rightBegin;

            return(leftBegin);
        }