Merge k Sorted Lists
2016-07-06 11:28
417 查看
/** * Definition for singly-linked list. * public class ListNode { * int val; * ListNode next; * ListNode(int x) { val = x; } * } */ public class Solution { public ListNode mergeKLists(ListNode[] lists) { if (lists == null) { throw new IllegalArgumentException("aa"); } if (lists.length == 0) { return null; } return helper(lists, 0, lists.length - 1); } private ListNode helper(ListNode[] lists, int start, int end) { if (start == end) { return lists[start]; } int mid = start + (end - start) / 2; ListNode left = helper(lists, start, mid); ListNode right = helper(lists, mid + 1, end); ListNode node = merge(left, right); return node; } private ListNode merge(ListNode left, ListNode right) { ListNode dummy = new ListNode(0); ListNode node = dummy; while (left != null && right != null) { if (left.val < right.val) { node.next = left; left = left.next; } else { node.next = right; right = right.next; } node = node.next; } if (left != null) { node.next = left; } if (right != null) { node.next = right; } return dummy.next; } }
相关文章推荐
- URAL 1309 Dispute <函数关系的递推>
- CSS布局之圣杯布局和双飞翼布局
- 【leetcode】264. Ugly Number II【M】【15】
- UML的六种关系
- poj 2823 Sliding Window 题解
- Delphi 设置时间格式
- bootstrap-table.js如何根据单元格数据不同显示不同的字体的颜色
- 剑指Offer:面试题13——在O(1)时间删除链表结点
- sql 金额以千分位逗号方式显示
- Springmvc中@RequestParam传值中文乱码解决方案
- cobbler的使用安装
- TOP-K问题-堆排序和快排实现
- InnoDB: The log sequence number in ibdatafiles does not match the log sequence number in the ib_log
- Android中资源文件用法简单示例
- qt 5.6+mingw 编译安装QWT
- <学习笔记> HttpClient工具类
- 非主流测试洞见:系统思考
- 深入理解原型和闭包
- poj 3071 Football dp(概率)
- CAS实现SSO单点登录原理