题解 | #单链表的排序#
单链表的排序
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) { // write code here ListNode pre=head; ListNode list=new ListNode(-1); ArrayList<Integer> arrayList=new ArrayList<Integer>(); while(pre!=null){ arrayList.add(pre.val); pre=pre.next; } arrayList.sort(Comparator.naturalOrder()); ListNode back=head; for(int i=0;i<arrayList.size();i++){ back.val=arrayList.get(i); back=back.next; } return head; } }