148. Sort List - LeetCode

Solution

148. Sort Listjava

Question

題目大意:對鏈表進行排序數組

思路:鏈表轉爲數組,數組用二分法排序ui

Java實現:code

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;
}
相關文章
相關標籤/搜索