Solution
Question
题目大意:对链表进行排序
思路:链表转为数组,数组用二分法排序
Java实现:
public ListNode sortList(ListNode head) {
// list to array
List<Integer> list = new ArrayList<>();
ListNode cur = head;
while (cur != null) {
list.add(cur.val);
cur = cur.next;
}
// quicksort
// quicksort(list);
Collections.sort(list);
// Arrays.sort(arr);
// array to list
cur = head;
for (int tmp : list) {
cur.val = tmp;
cur = cur.next;
}
return head;
}