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;
        }
    }
  • 相关阅读:
    做的一个HTML表白页面
    C语言--乱写C语言
    算法学习-带分数
    算法学习--兰顿蚂蚁
    算法学习---一个博弈问题
    数据结构学习--稀疏矩阵的三元组表示
    【Java初探外篇02】——关于静态方法与实例方法
    【Java初探实例篇01】——Java语言基础
    【教程知识向】——关于环境变量以及配置
    【java初探外篇01】——关于Java修饰符
  • 原文地址:https://www.cnblogs.com/airwindow/p/4207459.html
Copyright © 2011-2022 走看看