zoukankan      html  css  js  c++  java
  • [LeetCode#148]Sort List

    The problem:

    Sort a linked list in O(n log n) time using constant space complexity.

    My analysis:

    The is problem could be elegantly solved by using merge.

    The inefficient way is to scan the lists from the first list to the end list. However this way is too inefficient, each element in the list needed to be compared i - 1 times. (lists[i])

    An elegant way is to use the idea from merge sort. We could merge the lists in binary way.

    To achieve this goal, we need to define the recursion. (It includes some skills)

    1. we use two indexes: low and high, partition into two parts, then sort and each partition respectively.

    a. iff low < high, we partition the lists into <low .... mid> , <mid + 1 ....high>.

    b. iff low == high, there are only one list (lists[low]) left in the lists, we could directly return it. (base case)

    2. merge the two partitions, and return the result as one linked list.

    My solution:

    public class Solution {
        public ListNode sortList(ListNode head) {
            return mergeSort(head);
        }
        
        private ListNode mergeSort(ListNode head) {
            if (head == null || head.next == null) //the base case in the recursion is very important!
                return head;
                
            ListNode walker = head;
            ListNode runner = head;
            
            while (runner.next != null && runner.next.next != null) { //skill: check runner.next at first. 
                
                walker = walker.next; //this skill is amazing!!!
                runner = runner.next.next;
            }
        
            ListNode head2 = walker.next;
            walker.next = null;
            ListNode head1 = head;
            
            head1 = mergeSort(head1);
            head2 = mergeSort(head2);
            head = merge(head1, head2);
            
            return head;
        }
        
        private ListNode merge(ListNode head1, ListNode head2) {
            
            if (head1 == null && head2 == null)
                return null;
            
            if (head1 == null && head2 != null)
                return head2;
                
            if (head1 != null && head2 == null)
                return head1;
            
            ListNode dummy = new ListNode(0);
            ListNode pre = dummy;
    
            ListNode ptr1 = head1;
            ListNode ptr2 = head2;
            
            while (ptr1 != null && ptr2 != null) {
                
                if (ptr1.val <= ptr2.val) {
                    
                    pre.next = ptr1;
                    pre = pre.next; 
                    ptr1 = ptr1.next;
                } else {
                    
                    pre.next = ptr2;
                    pre = pre.next;
                    ptr2 = ptr2.next;
                }
            }
            
            if (ptr1 == null) 
                pre.next = ptr2;
            else 
                pre.next = ptr1;
        
            return dummy.next;
        }
    }
  • 相关阅读:
    cf round #421 div2 D. Mister B and PR Shifts
    cf round #421 div2 C. Mister B and Boring Game(trick)
    UVa 12716 GCD XOR
    cf 821E Okabe and El Psy Kongroo(矩阵快速幂)
    hdu 6109 数据分割(并查集+set)
    poj 2887 Big String(块状链表)
    hdu 6119 小小粉丝度度熊(区间双指针)
    hdu 6118 度度熊的交易计划(可行费用流)
    hdu 6015 Gameia(树上博弈)
    hdu 6096 String(AC自动机巧妙建图)
  • 原文地址:https://www.cnblogs.com/airwindow/p/4207459.html
Copyright © 2011-2022 走看看