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 idea behind this problem is easy : merge sort !
    But we should learn some tricky skills from this question.
    1. How to split a linked list into two separate linked lists?
    a. use two pointers: walker and runner.
    a.1 walker move one step in each iteration.
    a.2 runner move two steps in each iteration.
    b. iterate until runner can't move any more (runner.next == null || runner.next.next == null)
    c. then head1 is the head of original list, and head2 is the next element of runner.

    2. How to merge two linked list in an elegant way?
    The key idea behind writing an elegant code is always use the same invarint.
    To achieve this purpose, I use a dummy head as fake head and a pre pinter always point to the last element of the resulting list.
    ListNode dummy = new ListNode(0);
    pre = dummy;
    ...
    while () {
    pre.next = ...;
    }
    return dummy.next;

    My code:

    /**
     * Definition for singly-linked list.
     * class ListNode {
     *     int val;
     *     ListNode next;
     *     ListNode(int x) {
     *         val = x;
     *         next = null;
     *     }
     * }
     */
    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;
        }
    }
  • 相关阅读:
    (4.5.4)Android測试TestCase单元(Unit test)測试和instrumentationCase单元測试
    JVM、垃圾回收、内存调优、常见參数
    相关系数图可视化
    Codeforces 558(C、D、E)总结
    [Windows 64] (搬运)价值¥ 6,499的软件Navicat Premium11.2.11 最新版及其注册机
    ORA-12737: Instant Client Light: unsupported server character set CHS16GBK/ZHS16GBK解决方案
    计蒜客练习
    站在巨著之上谈泛型(转载)
    java util包概述
    Calender的使用详解
  • 原文地址:https://www.cnblogs.com/airwindow/p/4207337.html
Copyright © 2011-2022 走看看