优先队列偷懒
单链表的排序
http://www.nowcoder.com/practice/f23604257af94d939848729b1a5cda08
优先队列里放节点,然后一个一个取出来接到新链表后边(这样也算是交换节点而不是修改值,不过有一个坑需要注意,大家可以评论区说说哈哈哈)
```import java.util.*;
/*
* public class ListNode {
* int val;
* ListNode next = null;
* }
*/
public class Solution {
/**
*
* @param head ListNode类 the head node
* @return ListNode类
*/
public ListNode sortInList (ListNode head) {
// write code here
PriorityQueue<ListNode> priorityQueue = new PriorityQueue<>((l1,l2) -> l1.val-l2.val);
ListNode pre = head;
while (pre!=null) {
priorityQueue.offer(pre);
pre = pre.next;
}
ListNode res = new ListNode(0);
ListNode curr = res;
while (!priorityQueue.isEmpty()) {
curr.next = priorityQueue.poll();
curr = curr.next;
}
curr.next = null;
return res.next;
}
}