-
Notifications
You must be signed in to change notification settings - Fork 31
/
148.java
32 lines (30 loc) · 847 Bytes
/
148.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode() {}
* ListNode(int val) { this.val = val; }
* ListNode(int val, ListNode next) { this.val = val; this.next = next; }
* }
*/
class Solution {
public ListNode sortList(ListNode head) {
List<Integer> l = new ArrayList<>();
ListNode current = head;
while(current != null){
l.add(current.val);
current = current.next;
}
int[] array = l.stream().mapToInt(i->i).toArray();
Arrays.sort(array);
current = head;
int i=0;
while(current != null){
current.val = array[i];
i++;
current = current.next;
}
return head;
}
}