题解 | #单链表的排序#
单链表的排序
https://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) {
ListNode dummy = new ListNode(0);
ListNode pre = dummy;
PriorityQueue<Integer> que = new PriorityQueue<>();
ListNode cur = head;
while(cur != null){
que.add(cur.val);
cur = cur.next;
}
while(!que.isEmpty()){
ListNode temp = new ListNode(que.poll());
pre.next = temp;
pre = pre.next;
}
return dummy.next;
}
}