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;
        }
    }
  • 相关阅读:
    分布式解决方案的收集
    一天带你入门到放弃vue.js(三)
    一天带你入门到放弃vue.js(二)
    一天带你入门到放弃vue.js(一)
    JDK配置环境变量不成功的原因
    蚂蚁课堂(每特学院)-2期
    Java 使用blob对H5视频播放进行加密《java视频加密》
    Java 实现视频下载功能
    高并发与高可用实战之基础知识大型网站架构特征(一)
    Java线程池实现原理之自定义线程池(一)
  • 原文地址:https://www.cnblogs.com/airwindow/p/4207337.html
Copyright © 2011-2022 走看看