- /**
- * Definition for singly-linked list.
- * public class ListNode {
- * int val;
- * ListNode next;
- * ListNode(int x) { val = x; }
- * }
- */
- import java.util.LinkedList;
- import java.util.List;
- class Solution {
- public ListNode sortList(ListNode head) {
- if( head == null ){
- return head;
- }
- List<ListNode> list = new LinkedList<ListNode>();
- ListNode node = head;
- while( node!=null ){
- list.add(node);
- node = node.next;
- }
- list.sort(new Comparator<ListNode>() {
- @Override
- public int compare(ListNode o1, ListNode o2) {
- return o1.val> o2.val ? 1 : (o1.val == o2.val ? 0 : -1);
- }
- });
- head = new ListNode(0);
- ListNode r = head;
- for (ListNode n : list) {
- System.out.print(n.val + " ");
- r.next = n;
- r = n;
- }
- r.next = null;
- return head.next;
- }
- }
来源: http://www.bubuko.com/infodetail-3118051.html