import java.util.*;
/*
* public class ListNode {
* int val;
* ListNode next = null;
* }
*/
public class Solution {
public ListNode sortInList (ListNode head) {
//使用数组存储链表元素,然后排序,重新构造链表
ArrayList<Integer> list = new ArrayList<>();
ListNode cur = head;
while (cur != null) {
list.add(cur.val);
cur = cur.next;
}
//重新构造链表的值
cur = head;
//默认为升序排序
Collections.sort(list);
//遍历数组构造链表,改变值而已
for (Integer integer : list) {
cur.val = integer;
cur = cur.next;
}
return head;
}
}

京公网安备 11010502036488号