public SingleLL AddLast(SingleLL theList)
        {
            if (IsNullOrEmpty(theList))
            {
                return(this);
            }

            AddLast(theList.root);
            return(this);
        }
        public SingleLL AddFirst(SingleLL theList)
        {
            if (theList == null)
            {
                return(this);
            }

            theList.AddLast(this);
            root = theList.root;
            return(this);
        }
示例#3
0
        // Shorthand for more complex uses of AddTestRun().
        protected override void AddTestRun(params object[] args)
        {
            int[] valsOne = (int[])args[0];
            int[] valsTwo = (int[])args[1];
            int[] endVals = (int[])args[2];

            // Add the endVals list to the end of both valsOne and valsTwo
            SingleLL endList = new SingleLL(endVals);
            SingleLL listOne = new SingleLL(valsOne).AddLast(endList);
            SingleLL listTwo = new SingleLL(valsTwo).AddLast(endList);

            // The intersection point is the expected return value
            // If endVals was empty then endList.root will be null, for the case of having no intersection
            base.AddTestRun(new SingleLL[] { listOne, listTwo }, endList.root);
        }
        public static void PartitionList(int partVal, SingleLL list)
        {
            SingleLL.Node lowLead  = null;
            SingleLL.Node highRoot = null;
            SingleLL.Node currNode = list.root;

            while (currNode != null)
            {
                // Store for later because the if statement damages currNode.next connection
                SingleLL.Node nextNode = currNode.next;

                // High value, add currNode after highRoot
                if (currNode.val >= partVal)
                {
                    currNode.next = highRoot;
                    highRoot      = currNode;
                }
                // Low value, add currNode after lowLead
                else
                {
                    if (lowLead == null)
                    {
                        list.root = currNode;
                    }
                    else
                    {
                        lowLead.next = currNode;
                    }

                    lowLead = currNode;
                }

                currNode = nextNode;
            }

            // Link low and high sublists, or if no low values were found set root to be high sublist's root
            if (lowLead != null)
            {
                lowLead.next = highRoot;
            }
            else
            {
                list.root = highRoot;
            }
        }
示例#5
0
        public static SingleLL.Node FindIntersection(SingleLL listOne, SingleLL listTwo)
        {
            if (SingleLL.IsNullOrEmpty(listOne) || SingleLL.IsNullOrEmpty(listTwo))
            {
                return(null);
            }

            int lenOne = listOne.GetLength();
            int lenTwo = listTwo.GetLength();
            int lenDif = Math.Abs(lenOne - lenTwo);

            SingleLL.Node currNodeOne = listOne.root;
            SingleLL.Node currNodeTwo = listTwo.root;

            for (int i = 0; i < lenDif; i++)
            {
                if (lenOne > lenTwo)
                {
                    currNodeOne = currNodeOne.next;
                }
                else
                {
                    currNodeTwo = currNodeTwo.next;
                }
            }

            while (currNodeOne != null)
            {
                if (Object.ReferenceEquals(currNodeOne, currNodeTwo))
                {
                    return(currNodeOne);
                }

                currNodeOne = currNodeOne.next;
                currNodeTwo = currNodeTwo.next;
            }

            // No intersection found
            return(null);
        }
        public override bool Equals(object obj)
        {
            SingleLL that = (SingleLL)obj;

            if (IsEmpty() && that.IsEmpty())
            {
                return(true);
            }
            if (IsEmpty() || that.IsEmpty())
            {
                return(false);
            }

            Node currThis = root;
            Node currThat = that.root;

            while (currThis != null || currThat != null)
            {
                if (currThis == null)
                {
                    return(false);
                }
                if (currThat == null)
                {
                    return(false);
                }

                if (currThis.val != currThat.val)
                {
                    return(false);
                }

                currThis = currThis.next;
                currThat = currThat.next;
            }

            return(true);
        }
 public PartitionData(int thePartValue, SingleLL theList)
 {
     partValue = thePartValue;
     list      = theList;
 }
 public static bool IsNullOrEmpty(SingleLL theList)
 {
     return(theList == null || theList.IsEmpty());
 }