-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path148Sort_List.java
More file actions
34 lines (30 loc) · 903 Bytes
/
148Sort_List.java
File metadata and controls
34 lines (30 loc) · 903 Bytes
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
33
34
/**
* 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; }
* }
*/
//sort api, and then create new nodes, insert
class Solution {
public ListNode sortList(ListNode head) {
ListNode dummynode = new ListNode(0);
dummynode.next = head;
List<Integer> list = new ArrayList<Integer>();
while(head!= null){
list.add(head.val);
head=head.next;
}
Collections.sort(list);
//System.out.println(list);
ListNode temp = dummynode;
for(int i = 0; i<list.size(); i++){
temp.next = new ListNode(list.get(i));
temp = temp.next;
}
return dummynode.next;
}
}