Ejemplo n.º 1
0
        public static Node GetIntersection(Node list1, Node list2)
        {
            if (list1 == null || list2 == null)
            {
                return(null);
            }

            int  length1   = LinkedListHelper.GetLinkedListLength(list1);
            int  length2   = LinkedListHelper.GetLinkedListLength(list2);
            Node iterator1 = length1 <= length2 ? list1 : list2;
            Node iterator2 = length1 <= length2 ? list2 : list1;

            Node result    = null;
            Node iterator3 = result;

            while (iterator1 != null)
            {
                iterator2 = list2;
                while (iterator2 != null)
                {
                    if (iterator1.Value == iterator2.Value)
                    {
                        if (iterator3 == null)
                        {
                            iterator3 = new Node(iterator1.Value);
                            result    = iterator3;
                        }
                        else
                        {
                            iterator3.Next = new Node(iterator1.Value);
                            iterator3      = iterator3.Next;
                        }
                        iterator2 = iterator2.Next;
                        list1     = iterator1;
                        list2     = iterator2;
                        break;
                    }
                    else
                    {
                        if (result != null)
                        {
                            return(result);
                        }
                    }
                    iterator2 = iterator2.Next;
                }
                iterator1 = iterator1.Next;
            }
            return(result);
        }
Ejemplo n.º 2
0
        public static void RemoveDuplicateWithBuffer(ref Node list)
        {
            int        count    = 0;
            List <int> intList  = new List <int>();
            Node       iterator = list;

            while (iterator != null)
            {
                if (intList.Contains(iterator.Value))
                {
                    LinkedListHelper.DeleteNodeAtIndex(ref list, count);
                }
                else
                {
                    intList.Add(iterator.Value);
                    count++;
                }
                iterator = iterator.Next;
            }
        }
Ejemplo n.º 3
0
        public static Node Addition2_v2(Node h1, Node h2)
        {
            int val1 = 0, val2 = 0;
            int idx = LinkedListHelper.GetLinkedListLength(h1) - 1;

            while (h1 != null)
            {
                val1 += h1.Value * (int)Math.Pow(10, idx);
                idx--;
            }

            idx = LinkedListHelper.GetLinkedListLength(h2) - 1;
            while (h2 != null)
            {
                val2 += h2.Value * (int)Math.Pow(10, idx);
                idx--;
            }
            string result = (val1 + val2).ToString();

            Node headRes  = null;
            Node iterator = headRes;

            for (int i = 0; i < result.Length; i++)
            {
                if (headRes == null)
                {
                    headRes = new Node(result[i]);
                }
                else
                {
                    iterator.Next = new Node(result[i]);
                    iterator      = iterator.Next;
                }
            }
            return(headRes);
        }
Ejemplo n.º 4
0
        public static bool IsPalindrome_v1(Node head)
        {
            if (head == null || head.Next == null)
            {
                return(false);
            }

            Node iterator         = head;
            int  linkedListLength = LinkedListHelper.GetLinkedListLength(head);

            //Stack<int> intStack = new Stack<int>();
            int[] intStack = new int[linkedListLength / 2];
            int   count = 0, i = -1;
            bool  paired = linkedListLength % 2 == 0;

            if (paired)
            {
                while (iterator != null)
                {
                    if (count >= linkedListLength / 2)
                    {
                        if (intStack[i] != iterator.Value)
                        {
                            return(false);
                        }
                        i--;
                    }
                    else
                    {
                        intStack[count] = iterator.Value;
                        count++;
                        i++;
                    }
                    iterator = iterator.Next;
                }
                return(true);
            }
            else
            {
                bool first = true;
                while (iterator != null)
                {
                    if (count >= linkedListLength / 2)
                    {
                        if (first)
                        {
                            first = false;
                        }
                        else
                        {
                            if (intStack[i] != iterator.Value)
                            {
                                return(false);
                            }
                            i--;
                        }
                    }
                    else
                    {
                        intStack[count] = iterator.Value;
                        count++;
                        i++;
                    }
                    iterator = iterator.Next;
                }
                return(true);
            }
        }