Example #1
0
 public static bool IsPalindrome(ListNode head)
 {
     if (head == null || head.next == null)
     {
         return true;
     }
     tempHead = head;
     return IsPalin(head);
 }
Example #2
0
        public static ListNode merge(ListNode l1, ListNode l2)
        {
            ListNode l = new ListNode(0), p = l;

            while (l1 != null && l2 != null)
            {
                if (l1.val < l2.val)
                {
                    p.next = l1;
                    l1 = l1.next;
                }
                else
                {
                    p.next = l2;
                    l2 = l2.next;
                }
                p = p.next;
            }

            if (l1 != null)
                p.next = l1;

            if (l2 != null)
                p.next = l2;

            return l.next;
        }
Example #3
0
        private static void TestCases()
        {
            //QuickSort(new int[] { 2, 5, 1, 6, 7, 4 });
            //SummaryRanges(new int[] { 0, 1, 2, 4, 5, 7, 8 });
            //ThreeSum(new int[] { -1, 0, 1, 2, -1, -4 });

            //LargestNumber(new int[] { 3, 30, 34, 5, 9 });
            //int ele = FindKthLargest(new int[] { 2, 5, 1, 6, 7, 4 }, 3);

            ListNode head = new ListNode(1);
            ListNode p = head;
            p.next = new ListNode(2);
            for (int i = 2; i <= 3; i++)
            {
                p.next = new ListNode(i);
                p = p.next;
            }

            p.next = new ListNode(2);
            p = p.next;
            p.next = new ListNode(1);
            IsPalindrome(head);
            ListNode q = head;

            q = q.next;
            double x = Math.Pow(3, 2);
        }
Example #4
0
        private static bool IsPalin(ListNode head)
        {
            //current node is the last one
            if (head.next == null)
            {
                return head.val == tempHead.val;
            }

            //when return true, head is the last second one
            bool res = IsPalin(head.next);
            tempHead = tempHead.next;
            return res && head.val == tempHead.val;
        }
Example #5
0
        public static ListNode sortList(ListNode head)
        {
            if (head == null || head.next == null)
                return head;

            // step 1. cut the list to two halves
            ListNode prev = null, slow = head, fast = head;

            while (fast != null && fast.next != null)
            {
                prev = slow;
                slow = slow.next;
                fast = fast.next.next;
            }

            prev.next = null;

            // step 2. sort each half
            ListNode l1 = sortList(head);
            ListNode l2 = sortList(slow);

            // step 3. merge l1 and l2
            return merge(l1, l2);
        }
Example #6
0
        public static void ReorderList(ListNode head)
        {
            ListNode lN = head;
            ListNode fast = head;
            ListNode slow = head;
            ListNode pre = null;

            while (fast != null && fast.next != null)
            {
                pre = slow;
                slow = slow.next;
                fast = fast.next.next;
            }

            //reverse from slow to fast
            ListNode dummy = null;
            ListNode cur = slow;
            while (cur != null)
            {
                ListNode next = cur.next;
                cur.next = dummy;
                dummy = cur;
                cur = next;
            }

            //merge two part of link list
            ListNode rN = dummy;
            ListNode newHead = lN;
            while (lN != null)
            {
                ListNode lnNext = lN.next;
                ListNode rnNext = rN.next;

                lN.next = rN;
                //left part to the end, then right part just leave it as it
                if (lnNext == null)
                {
                    break;
                }

                rN.next = lnNext;

                lN = lnNext;
                rN = rnNext;
            }
        }