Example #1
0
        public LinkedList.LinkedNode reverseBetween(LinkedList.LinkedNode head, int m, int n)
        {
            LinkedList.LinkedNode pre_hard = new LinkedNode(-1);
            pre_hard.Next = head;
            LinkedList.LinkedNode pre = pre_hard, cur = head, next = head.Next, lefts = null, lefte = null, rights = null, righte;
            int count = m - 1;

            while (count-- > 0)
            {
                next = cur.Next;
                pre  = cur;
                cur  = next;
            }
            count = n - m + 1;
            while (count-- > 0)
            {
                next = cur.Next;
                if (lefte == null)
                {
                    lefte = cur;
                    lefts = pre;
                }
                else
                {
                    cur.Next = pre;
                }
                pre = cur;
                cur = next;
            }
            rights     = pre;
            righte     = cur;
            lefts.Next = rights;
            lefte.Next = righte;
            return(pre_hard.Next);
        }
Example #2
0
        public static LinkedList.LinkedNode findCommonNode(LinkedList.LinkedNode ln1, LinkedList.LinkedNode ln2)
        {
            LinkedList.LinkedNode Runner1 = ln1;
            LinkedList.LinkedNode Runner2 = ln2;

            int length1 = ln1.getLength();
            int length2 = ln2.getLength();

            if (length1 > length2)
            {
                for (int i = 0; i < length1 - length2; i++)
                {
                    Runner1 = Runner1.Next;
                }
            }
            else
            {
                for (int i = 0; i < length2 - length1; i++)
                {
                    Runner2 = Runner2.Next;
                }
            }


            while (Runner1 != Runner2)
            {
                Runner1 = Runner1.Next;
                Runner2 = Runner2.Next;
            }
            return(Runner1);
        }
Example #3
0
        public static void printBackFordLinkedListEx(LinkedList.LinkedNode head)
        {
            if (head.Next != null)
            {
                printBackFordLinkedListEx(head.Next);
            }

            System.Console.WriteLine(head.Data);
        }
Example #4
0
        public static LinkedList.LinkedNode printBackFordLinkedList(LinkedList.LinkedNode head)
        {
            if (head == null)
            {
                return(head);
            }
            LinkedList.LinkedNode node = printBackFordLinkedList(head.Next);

            System.Console.WriteLine(head.Data);

            return(node);
        }
Example #5
0
 public void enqueue(double val)
 {
     if (head == null)
     {
         head = new LinkedList.LinkedNode(val);
         tail = head;
         return;
     }
     LinkedList.LinkedNode ln = new LinkedList.LinkedNode(val);
     ln.Next = head;
     head = ln;
 }
Example #6
0
 public void enqueue(double val)
 {
     if (head == null)
     {
         head = new LinkedList.LinkedNode(val);
         tail = head;
         return;
     }
     LinkedList.LinkedNode ln = new LinkedList.LinkedNode(val);
     ln.Next = head;
     head    = ln;
 }
Example #7
0
        public double dequeue()
        {
            double val = tail.Data;

            LinkedList.LinkedNode ln = head;
            while (ln.Next != null && ln.Next.Next != null)
            {
                ln = ln.Next;
            }
            ln.Next = null;
            tail = ln;
            return val;
        }
Example #8
0
        public double dequeue()
        {
            double val = tail.Data;

            LinkedList.LinkedNode ln = head;
            while (ln.Next != null && ln.Next.Next != null)
            {
                ln = ln.Next;
            }
            ln.Next = null;
            tail    = ln;
            return(val);
        }
Example #9
0
        public static int getNthToLastRecursiveEx2(LinkedNode head, int n)
        {
            if (head == null)
            {
                return(0);
            }
            int i = getNthToLastRecursiveEx2(head.Next, n) + 1;

            if (i == n)
            {
                result = head;
            }

            return(i);
        }
Example #10
0
        public static int getNthToLastRecursiveEx2(LinkedNode head, int n)
        {
            if (head == null)
            {
                return 0;
            }
            int i = getNthToLastRecursiveEx2(head.Next, n) + 1;

            if (i == n)
            {
                result = head;
            }

            return i;
        }
Example #11
0
        public static LinkedList.LinkedNode getLastNthLinklistNode(LinkedList.LinkedNode node, int n)
        {
            if (node == null)
            {
                return(node);
            }

            LinkedList.LinkedNode result = getLastNthLinklistNode(node.Next, n);

            if (count++ == n)
            {
                return(node);
            }

            return(result);
        }
Example #12
0
        //By state
        public static LinkedList.LinkedNode getCrossForTwoLinkedList(LinkedList.LinkedNode ln1, LinkedList.LinkedNode ln2)
        {
            LinkedList.LinkedNode ln = ln1;
            while (ln != null && ln.state != 1)
            {
                ln.state = 1;
                ln       = ln.Next;
            }

            ln = ln2;
            while (ln != null)
            {
                if (ln.state == 1)
                {
                    return(ln);
                }
                ln = ln.Next;
            }
            return(null);
        }
Example #13
0
        public static LinkedList.LinkedNode getCrossForTwoLinkedListEx(LinkedList.LinkedNode ln1, LinkedList.LinkedNode ln2)
        {
            LinkedList.LinkedNode loop_ln1 = ln1;
            int ln1_length = 0;

            while (loop_ln1 != null)
            {
                ln1_length++;
                loop_ln1 = loop_ln1.Next;
            }
            LinkedList.LinkedNode loop_ln2 = ln2;
            int ln2_length = 0;

            while (loop_ln2 != null)
            {
                ln2_length++;
                loop_ln2 = loop_ln2.Next;
            }
            if (loop_ln1 == loop_ln2)
            {
                LinkedList.LinkedNode longer_list  = ln1_length > ln2_length ? ln1 : ln2;
                LinkedList.LinkedNode smaller_list = ln1_length < ln2_length ? ln1 : ln2;
                for (int i = 0; i < System.Math.Abs(ln1_length - ln2_length); i++)
                {
                    longer_list = longer_list.Next;
                }
                while (true)
                {
                    if (longer_list == smaller_list)
                    {
                        return(longer_list);
                    }
                    longer_list  = longer_list.Next;
                    smaller_list = smaller_list.Next;
                }
            }
            return(null);
        }
Example #14
0
        public static void printSeriesLinkedList(LinkedList.LinkedNode ln1, LinkedList.LinkedNode ln2)
        {
            LinkedList.LinkedNode ln_loop1 = ln1;
            LinkedList.LinkedNode ln_loop2 = ln2;

            while (ln_loop1 != null && ln_loop2 != null)
            {
                if (ln_loop1.Data < ln_loop2.Data)
                {
                    System.Console.WriteLine(ln_loop1.Data);
                    ln_loop1 = ln_loop1.Next;
                }
                else if (ln_loop1.Data == ln_loop2.Data)
                {
                    System.Console.WriteLine(ln_loop1.Data);
                    ln_loop1 = ln_loop1.Next;
                    ln_loop2 = ln_loop2.Next;
                }
                else
                {
                    System.Console.WriteLine(ln_loop2.Data);
                    ln_loop2 = ln_loop2.Next;
                }
            }

            while (ln_loop1 != null)
            {
                System.Console.WriteLine(ln_loop1.Data);
                ln_loop1 = ln_loop1.Next;
            }

            while (ln_loop2 != null)
            {
                System.Console.WriteLine(ln_loop2.Data);
                ln_loop2 = ln_loop2.Next;
            }
        }
Example #15
0
        public static LinkedList.LinkedNode findNthBackWard(LinkedList.LinkedNode head, int n)
        {
            LinkedList.LinkedNode ToEnd    = head;
            LinkedList.LinkedNode ToTarget = head;

            while (n-- != 0)
            {
                if (ToEnd == null)
                {
                    return(null);
                }
                ToEnd = ToEnd.Next;
            }

            while (true)
            {
                if (ToEnd.Next == null)
                {
                    return(ToTarget);
                }
                ToTarget = ToTarget.Next;
                ToEnd    = ToEnd.Next;
            }
        }