146. LRU 缓存
小于 1 分钟
146. LRU 缓存中等
解法:遍历
class Solution {
public ListNode sortList(ListNode head) {
if(head == null) return null;
ListNode node = head;
ArrayList<Integer> list = new ArrayList<>();
while(node != null) {
list.add(node.val);
node = node.next;
}
Object[] array = list.toArray();
Arrays.sort(array);
ListNode res = new ListNode((int)array[0]);
ListNode idx = res;
for (int i = 1; i < array.length; i++) {
idx.next = new ListNode((int)array[i]);
idx = idx.next;
}
return res;
}
}
Powered by Waline v2.15.5