import java.util.*;

/*
 * public class ListNode {
 *   int val;
 *   ListNode next = null;
 * }
 */

public class Solution {
    /**
     * 
     * @param head ListNode类 the head node
     * @return ListNode类
     */
	public ListNode sortInList(ListNode head) {
        // write code here
        if (head == null || head.next == null) {
            return head;
        }

        ListNode h = head;
        ArrayList<Integer> list = new ArrayList<>(1000);
        while (h != null) {
            list.add(h.val);
            h = h.next;
        }
        list.sort(Comparator.naturalOrder());

        ListNode res = new ListNode(-1);
        ListNode p = res;
        for (Integer integer : list) {
            p.next = new ListNode(integer);
            p = p.next;
        }
        return res.next;
    }
}