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;
        }
    }
  • 相关阅读:
    shell加密为二进制可执行文件
    lua自写限制并发访问模块
    centos虚拟机中挂新硬盘
    lua文件修改为二进制文件
    常用一些命令方法
    常用问题处理方法
    CVE-2010-3333
    Kernel Stack Overflow(转)
    NULL Pointer Dereference(转)
    Linux内核漏洞利用-环境配置(转)
  • 原文地址:https://www.cnblogs.com/airwindow/p/4207459.html
Copyright © 2011-2022 走看看