题解 | #单链表的排序#
单链表的排序
https://www.nowcoder.com/practice/f23604257af94d939848729b1a5cda08
// 我这里直接让Java帮我们排序。
// 转化为列表,然后调用列表的排序方法
import java.util.*;
public class Solution {
public ListNode sortInList (ListNode head) {
ListNode res = new ListNode(-1);
res.next = head;
ArrayList<Integer> list = new ArrayList<>();
while(head!=null){
list.add(head.val);
head = head.next;
}
list.sort(Integer::compareTo);
ListNode temp = res;
for(int i = 0; i < list.size(); i++){
temp.next = new ListNode(list.get(i));
temp = temp.next;
}
return res.next;
}
}
