public int connectedG(listnode head, int[] G)
        {
            if (head == null || G == null)
            {
                return(-1);
            }
            HashSet <int> setG = new HashSet <int>();

            foreach (int val in G)
            {
                setG.Add(val);
            }
            int  count = 0;
            bool exist = false;

            while (head != null)
            {
                if (setG.Contains(head.Val))
                {
                    if (!exist)
                    {
                        exist = true;
                        count++;
                    }
                }
                else
                {
                    exist = false;
                }
                head = head.Next;
            }
            return(count);
        }
        public listnode removeNthElementFromLast(listnode LK, int n)
        {
            if (LK == null)
            {
                return(null);
            }
            if (n == 0)
            {
                return(LK);
            }

            listnode pre = new listnode(0);

            pre.Next = LK;
            listnode R1 = pre;
            listnode R2 = pre;

            int i = 0;

            while (i < n)
            {
                R1 = R1.Next;
                i++;
            }
            while (R1 != null)
            {
                R1 = R1.Next;
                R2 = R2.Next;
            }
            R2.Next = R2.Next.Next;

            return(LK);
        }
        public listnode cycleStartAt(listnode head)
        {
            if (head == null)
            {
                return(null);
            }

            listnode slow = head;
            listnode fast = head;

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


            return(null);
        }
        //Two pointers
        // note: the idea is to swap element k steps ahead each other,
        // after swapping, if the total element count is odd middle will left alone and
        // slow will be exactly at the middle so move the middle to the end by swaping it with the next element
        public listnode rotate(listnode head, int k)
        {
            if (head == null)
            {
                return(null);
            }
            if (k == 0)
            {
                return(head);
            }

            // what if K > head length

            listnode dummy = new listnode(0);

            dummy = head;
            listnode slow = dummy;
            listnode fast = dummy; // after K steps

            int count = 0;

            while (fast != null)
            {
                if (count > k)
                {
                    break;
                }
                else
                {
                    count++;
                    fast = fast.Next; // fast is K steps ahead of slow
                }
            }

            while (fast != null)
            {
                int tmp = slow.Val;
                slow.Val = fast.Val;
                fast.Val = tmp;
                slow     = slow.Next;
                fast     = fast.Next;
            }


            if (count % 2 > 0)                // if the list is odd, the middle node we left alone
            {
                while (slow.Next != null)     //slow is now at the middle, send the middle node to the last
                {
                    int tmp2 = slow.Val;
                    slow.Val      = slow.Next.Val;
                    slow.Next.Val = tmp2;
                    slow          = slow.Next;
                }
            }



            return(dummy.Next);
        }
示例#5
0
        public listnode reverse(listnode head, int m, int n)
        {
            if (m == n)
            {
                return(head);
            }
            listnode mPrev     = null;
            listnode nAfter    = null;
            listnode mPrevPrev = null;
            listnode tmp       = head;

            //reverse
            int i = 1;

            while (tmp != null && i <= n)
            {
                if (n == m - 1)
                {
                    mPrevPrev = tmp;
                }
                if (i == m)
                {
                    mPrev = tmp;
                }
                if (i == n)
                {
                    nAfter = tmp.Next;
                }
            }
            ++i;
            tmp = tmp.Next;

            if (mPrev.Next == null)
            {
                return(head);
            }

            listnode p1 = mPrev;
            listnode p2 = p1.Next;

            while (p1 != null && p2 != null)
            {
                listnode t = p2.Next;
                p2.Next = p1;
                p1      = p2;
                p2      = t;
            }
            if (mPrevPrev != null)
            {
                mPrevPrev.Next = p1;                         //connecting
            }
            else
            {
                return(p1);
            }

            return(head);
        }
        public listnode reverse(listnode head)
        {
            listnode prev = null;
            listnode curr = head;
            listnode nxt  = head;

            while (curr != null)
            {
                nxt       = curr.Next;
                curr.Next = prev;

                prev = curr;
                curr = nxt;
            }
            return(prev);
        }
        public listnode merge(listnode l1, listnode l2)
        {
            if (l1 == null && l2 == null)
            {
                return(null);
            }
            else if (l1 == null)
            {
                return(l2);
            }
            else if (l2 == null)
            {
                return(l1);
            }

            listnode head  = new listnode(0);
            listnode dummy = head;

            while (l1 != null && l2 != null)
            {
                if (l1.Val <= l2.Val)
                {
                    dummy.Next = l1;
                    l1         = l1.Next;
                }
                else
                {
                    dummy.Next = l2;
                    l2         = l2.Next;
                }
                dummy = dummy.Next;
            }

            while (l1 != null)   // a's remaining elements
            {
                dummy.Next = l1;
                l1         = l1.Next;
            }
            while (l2 != null)  // b's remaining elements
            {
                dummy.Next = l2;
                l2         = l2.Next;
            }


            return(head.Next);
        }
        public bool hasCycle(listnode head)
        {
            if (head == null)
            {
                return(false);
            }

            listnode slow = head;
            listnode fast = head;

            while (slow != null && fast.Next != null) // for cyclic linkedlist neither should be null
            {
                slow = slow.Next;
                fast = fast.Next.Next;
                if (slow == fast)
                {
                    return(true);
                }
            }
            return(false);
        }
        public listnode addTwo(listnode l1, listnode l2)
        {
            if (l1 == null && l2 == null)
            {
                return(null);
            }
            if (l1 == null)
            {
                return(l2);
            }
            if (l2 == null)
            {
                return(l1);
            }

            listnode p1      = l1;
            listnode p2      = l2;
            int      countL1 = 0;
            int      countL2 = 0;
            int      sum     = 0;

            while (l1 != null)
            {
                countL1++;
                l1 = l1.Next;
            }
            while (l1 != null)
            {
                countL2++;
                l2 = l2.Next;
            }

            if (countL1 > countL2)
            {
                while (countL2 == countL1)
                {
                    p2.Val = 0;
                    p2     = p2.Next;
                    countL2++;
                }
                p2.Next = l1;
            }
            if (countL2 > countL1)
            {
                while (countL1 == countL2)
                {
                    p1.Val = 0;
                    p1     = p2.Next;
                    countL1++;
                }
                p1.Next = l1;
            }

            listnode addedLst = p1;
            int      carry    = 0;

            while (p1 != null)
            {
                if (p1.Val + p2.Val + carry > 10)
                {
                    sum = (p1.Val + p2.Val) % 10;
                    carry++;
                }
                else
                {
                    sum = (p1.Val + p2.Val + carry);
                }
                addedLst.Val = sum;
                addedLst     = addedLst.Next;
                p1           = p1.Next;
                p2           = p2.Next;
            }
            return(addedLst);
        }