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;
        }
    }
  • 相关阅读:
    Atom使用教程
    4-[函数]-参数
    4-[函数]- 独立功能的代码块
    3 [文件]-修改文件
    2 [文件]-文件操作
    1. [文件]- 文件类型,文件open模式
    2-16 阶段考核
    react native 网络get请求方式参数不可为undefined或null
    [网络]远程访问局域网svn服务器[转]
    react native listview 一个有用的属性,用作两列布局
  • 原文地址:https://www.cnblogs.com/airwindow/p/4207337.html
Copyright © 2011-2022 走看看