Java实现-链表排序
2017-06-08 22:37
309 查看
/**
* Definition for ListNode.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int val) {
* this.val = val;
* this.next = null;
* }
* }
*/
public class Solution {
/**
* @param head: The head of linked list.
* @return: You should return the head of the sorted linked list,
using constant space complexity.
*/
public ListNode sortList(ListNode head) {
// write your code here
if(head==null){
return null;
}
List<Integer> list=new ArrayList<Integer>();
list.add(head.val);
while(head.next!=null){
head=head.next;
list.add(head.val);
}
Collections.sort(list);
ListNode h=new ListNode(list.get(0));
ListNode node=h;
for(int i=1;i<list.size();i++){
node.next=new ListNode(list.get(i));
node=node.next;
}
return h;
}
}
相关文章推荐
- 数据结构学习----线性表的链式表示之升序排序的单链表(Java实现)
- 【LeetCode-面试算法经典-Java实现】【109-Convert Sorted List to Binary Search Tree(排序链表转换成二叉排序树)】
- Java实现-合并两个排序链表
- java实现合并两排序链表
- 剑指Offer--面试题17:合并两个排序的链表--Java实现
- JAVA实现合并两个排序的链表(《剑指offer》)
- 【LeetCode-面试算法经典-Java实现】【109-Convert Sorted List to Binary Search Tree(排序链表转换成二叉排序树)】
- 剑指offer 面试题17 合并两个排序的链表-Java实现
- 【LeetCode-面试算法经典-Java实现】【082-Remove Duplicates from Sorted List II(排序链表中删除重复元素II)】
- (Java实现)输入一棵二叉搜索树,将该二叉搜索树转换成一个排序的双向链表。要求不能创建任何新的结点,只能调整树中结点指针的指向
- 快速排序 Java实现-数组-链表
- java实现单向链表CRUD,反转,排序,查找倒数第k个元素,递归输出等操作
- 把二元查找树转变成排序的双向链表——Java实现
- 【LeetCode-面试算法经典-Java实现】【083-Remove Duplicates from Sorted List(排序的单链表中删除重复的结点)】
- 数据结构与算法分析笔记与总结(java实现)--链表8:对排序链表删除重复结点问题
- Java实现链表的插入,删除,排序,输出
- 剑指Offer 面试题25:合并两个排序的链表(递归+非递归) Java代码实现
- 合并两个排序的链表java实现
- Java实现-删除排序链表中的重复元素2
- 经典面试题:Java实现-归并K条已排序的链表及复杂度分析、优化