您的位置:首页 > 编程语言 > Java开发

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;

}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  LintCode链表排序
相关文章推荐