Leetcode Sort List
来源:互联网 发布:淘宝佣金软件购买 编辑:程序博客网 时间:2024/04/26 13:53
题目:Sort a linked list in O(n log n) time using constant space complexity.
答案,源代码:
/**
* Definition for singly-linked list.
* class ListNode {
* int val;
* ListNode next;
* ListNode(int x) {
* val = x;
* next = null;
* }
* }
*/
public class Solution {
答案,源代码:
/**
* 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) {
if(null==head){return head;}ArrayList<ListNode> al = new ArrayList<ListNode>();ListNode ln =head;al.add(ln);while (null != ln.next) {ln=ln.next;al.add(ln);}Collections.sort(al, new Comparator<ListNode>() {@Overridepublic int compare(ListNode node1, ListNode node2) {return node1.val-node2.val;}});ln=al.get(0);ListNode node = ln;for (int i = 1; i < al.size(); i++) {node.next = new ListNode(0);node.next.val=al.get(i).val;node=node.next;}return ln;
}
}
0 0
- [leetcode][list][sort] Sort List
- Insertion Sort List | leetcode
- Leetcode: Insertion Sort List
- [LeetCode] Insertion Sort List
- Leetcode: Insertion Sort List
- Leetcode: Sort List
- Sort List | leetcode
- leetcode-Sort List
- leetcode-Insertion Sort List
- LeetCode题解:Sort List
- leetcode 134: Sort List
- [LeetCode]Sort List
- [LeetCode]Insertion Sort List
- [LeetCode] Sort List
- 【LeetCode】Insertion Sort List
- leetCode - Sort List
- LeetCode | Insertion Sort List
- LeetCode - Insertion Sort List
- 部署asp.net网站时出现“未在本地计算机上注册 Microsoft.Jet.OLEDB.4.0 提供程序”
- Android Bluetooth蓝牙开发\蓝牙协议\蓝牙通信例子_Android支持蓝牙4.0版本_BLE开发
- 解决MySql Error Code: 2006 – MySQL服务器已离线错误
- HDU 2399 GPA(简单字符串的处理)
- oracle查询所有所有的表结构及字段描述等信息并plsql保存查询结果
- Leetcode Sort List
- Apache Commons Pool试用小记
- GCD(Grand Central Dispatch)教程
- XTPToolKitPro
- jquery确认框和消息提示
- 如何做一个应用程序的快捷方式入口
- application Assembly里少J2EE modules
- Equals 方法和 HashCode方法
- 设计模式及各大原则简介