题解 | #单链表的排序#
单链表的排序
https://www.nowcoder.com/practice/f23604257af94d939848729b1a5cda08
import java.util.*; /* * public class ListNode { * int val; * ListNode next = null; * public ListNode(int val) { * this.val = val; * } * } */ public class Solution { public ListNode sortInList (ListNode head) { PriorityQueue<ListNode> queue=new PriorityQueue<>((o1,o2)->(o1.val-o2.val)); while(head!=null){ queue.offer(head); head=head.next; } ListNode node=new ListNode(-1); ListNode cur=node; while(!queue.isEmpty()){ ListNode list=queue.poll(); list.next=null; node.next=list; node=node.next; } return cur.next; } }