Пример #1
0
        static private void ReverseList(MyLinkedList <int> l)
        {
            MyNode <int> previous, current, next;

            previous = null;
            current  = l.First;
            next     = current.Next;

            while (next != null)
            {
                current.Next = previous;
                previous     = current;
                current      = next;
                next         = next.Next;
            }
            current.Next = previous;
            l.First      = current;
        }
Пример #2
0
        static public void SwapPairwise(MyLinkedList <int> l)
        {
            Console.WriteLine("Original list: ");
            l.Print();
            var a = l.First;

            if (l == null || a.Next == null)
            {
                return;
            }

            l.First = a.Next;

            //list with 2 elements
            if (a.Next.Next == null)
            {
                a.Next.Next = a;
                a.Next      = null;
                return;
            }

            //for more than 2 elements. A and B in position

            var b = a.Next.Next;

            while (a.Next.Next != null)
            {
                a.Next.Next = a;
                if (b.Next == null) // odd number of elements
                {
                    a.Next = b;
                    break;
                }
                a.Next = b.Next;
                if (b.Next.Next == null)// even number of elements
                {
                    b.Next.Next = b;
                    b.Next      = null;
                    break;
                }
                a = b;
                b = a.Next.Next;
            }
        }
Пример #3
0
        static public MyLinkedList <int> Merge(MyLinkedList <int> a, MyLinkedList <int> b)
        {
            a.Print();
            b.Print();


            //vamos arevisar cada lista y poner los elementos en la lista que tenga el elemento m[as pequeli

            MyNode <int> x;

            if (b.First.Value <= a.First.Value)
            {
                x       = b.First;
                b.First = b.First.Next;
                x.Next  = a.First;
                a.First = x;
            }

            var current = a.First;

            while (b.First != null && current.Next != null)
            {
                if (b.First.Value < current.Next.Value)
                {
                    x       = b.First;
                    b.First = b.First.Next;

                    x.Next       = current.Next;
                    current.Next = x;
                }
                current = current.Next;
            }
            if (b.First != null)// this element should be at the end of a
            {
                current.Next = b.First;
                b.First      = null;
            }
            Console.Write("List 1: ");
            a.Print();
            Console.Write("List 2: ");
            b.Print();
            return(a);
        }
Пример #4
0
        /// <summary>
        /// Given a linked list, check if the the linked list has a loop. Linked list can contain self loop.
        /// </summary>
        /// <param name="l"></param>
        /// <returns></returns>
        static public bool HasLoop(MyLinkedList <int> l)
        {
            var oneSteps = l.First;
            var twoSteps = l.First.Next;

            while (oneSteps != null && twoSteps != null)
            {
                if (oneSteps.Value == twoSteps.Value)
                {
                    return(true);
                }
                oneSteps = oneSteps.Next;
                twoSteps = twoSteps.Next;
                if (twoSteps.Next == null)
                {
                    return(false);
                }
                twoSteps = twoSteps.Next;
            }

            return(false);
        }
Пример #5
0
        /// <summary>
        /// Given two numbers represented by two lists, write a function that returns sum list.
        /// The sum list is list representation of addition of two input numbers.
        /// Suppose you have two linked list 5->4 ( 4 5 )and 5->4->3( 3 4 5) you have to return  a resultant linked list 0->9->3 (3 9 0).
        /// </summary>
        /// <param name="l1"></param>
        /// <param name="l2"></param>
        static public void SUM(MyLinkedList <int> l1, MyLinkedList <int> l2)
        {
            int carry = 0;
            int suma  = 0;

            var current1  = l1.First;
            var current2  = l2.First;
            var previous1 = current1;
            var previous2 = current2;

            while (current1 != null && current2 != null)
            {
                suma           = current1.Value + current2.Value;
                current2.Value = (suma % 10) + carry;
                carry          = suma / 10;

                previous1 = current1;
                current1  = current1.Next;

                previous2 = current2; //if list one has fewer number of elem
                current2  = current2.Next;
            }

            if (current1 == null && current2 != null)
            {
                current2.Value += carry;
            }

            if (current1 != null && current2 == null)
            {
                current1.Value += carry;
                previous2.Next  = current1;
                previous1.Next  = null;
            }
            l1.First = null;
            l2.Print();
        }
Пример #6
0
        static public void ReverseKNodes(MyLinkedList <int> l, int k) //k>0
        {
            if (l == null || l.First == null)
            {
                Console.WriteLine("Empty list");
                return;
            }
            if (k == 1 || l.First.Next == null) //the result is the same list
            {
                return;
            }
            if (k == 2 && l.First.Next.Next == null)//k==2 and two element in the list, the result is the reversed list
            {
                ReverseList(l);
                return;
            }

            MyNode <int> leader       = l.First; // this pointer is counting k nodes
            int          counter      = 1;
            bool         firstChanged = false;

            MyNode <int> a = l.First, b = null, x = null;

            while (leader.Next != null && counter <= k)
            {
                if (counter == k)
                {
                    if (!firstChanged) //changing the head of the list
                    {
                        l.First      = leader;
                        firstChanged = true;
                    }
                    b      = leader;
                    leader = leader.Next;
                    ReverseNode(a, k);
                    if (x == null)
                    {
                        x = a;
                    }
                    else
                    {
                        x.Next = b;
                        x      = a;
                    }
                    a       = leader;
                    counter = 1;
                }

                if (leader.Next == null)
                {
                    x.Next = a;
                }
                if (leader.Next.Next == null)
                {
                    x.Next = leader.Next;
                    ReverseNode(a, k);
                    break;
                }
                leader = leader.Next;
                counter++;
            }
        }