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;
        }
    }
  • 相关阅读:
    unity c# 获取系统时间
    如果你想让继承MonoBehaviour的类变成Singleten
    关于程序员
    开始养成记录的习惯吧
    关于结构体的赋值问题
    数学中的集合,群,环,域
    励志
    [编程题] 进制均值
    javaEE 入门
    jsp内置对象2
  • 原文地址:https://www.cnblogs.com/airwindow/p/4207459.html
Copyright © 2011-2022 走看看