zoukankan      html  css  js  c++  java
  • [LeetCode#23]Merge k Sorted Lists

    The problem:

    Merge k sorted linked lists and return it as one sorted list. Analyze and describe its 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 repsectively.
        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 first solution:

    public class Solution {
        public ListNode mergeKLists(List<ListNode> lists) {
            if (lists == null || lists.size() == 0)
                return null;
            return helper(lists, 0, lists.size()-1);
        }
        
        private ListNode helper(List<ListNode> lists, int start, int end) {
            if (start == end)
                return lists.get((start + end)/2);
            int mid = (start + end)/2;
            ListNode l1 = helper(lists, start, mid);
            ListNode l2 = helper(lists, mid+1, end);
            ListNode new_list = merge(l1, l2);
            return new_list;
        }
        
        private ListNode merge(ListNode head1, ListNode head2) {
            if (head1 == null)
                return head2;
            if (head2 == null)
                return head1;
            ListNode l1 = head1;
            ListNode l2 = head2;
            ListNode dummy = new ListNode(0);
            ListNode pre = dummy;
            while (l1 != null && l2 != null) {
                if (l1.val < l2.val) {
                    ListNode next1 = l1.next;
                    l1.next = pre.next;
                    pre.next = l1;
                    pre = l1;
                    l1 = next1;
                } else{
                    ListNode next2 = l2.next;
                    l2.next = pre.next;
                    pre.next = l2;
                    pre = l2;
                    l2 = next2;
                }
            }
            if (l1 == null)
                pre.next = l2;
            else 
                pre.next = l1;
            return dummy.next;
        }
    }
  • 相关阅读:
    (原)Lazarus 异构平台下多层架构思路、DataSet转换核心代码
    (学)新版动态表单研发,阶段成果3
    (学) 如何将 Oracle 序列 重置 清零 How to reset an Oracle sequence
    (学)XtraReport WebService Print 报错
    (原)三星 i6410 刷机 短信 无法 保存 解决 办法
    (原) Devexpress 汉化包 制作工具、测试程序
    linux下网络配置
    apache自带ab.exe小工具使用小结
    Yii::app()用法小结
    PDO使用小结
  • 原文地址:https://www.cnblogs.com/airwindow/p/4257744.html
Copyright © 2011-2022 走看看