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
        // ListNode newHead = new ListNode(-1);
        // newHead.next = head;
        // ListNode temp = head;
        // int length = 0;
        // while (temp != null) {
        //     length++;
        //     temp = temp.next;
        // }
        // while (length > 0) {
        //     ListNode pre = newHead;
        //     ListNode cur = newHead.next;
        //     ListNode next = cur.next;
        //     boolean flag = false;
        //     while (next != null) {
        //         if (next.val < cur.val) {
        //             cur.next = next.next;
        //             next.next = cur;
        //             pre.next = next;
        //             flag = true;
        //         }
        //         pre = pre.next;
        //         cur = pre.next;
        //         next = cur.next;
        //     }
        //     if (!flag) {
        //         break;
        //     }
        //     length--;
        // }
        // return newHead.next;
        List<Integer> list = new ArrayList<>();
        while (head != null) {
            list.add(head.val);
            head = head.next;
        }
        Collections.sort(list);
        ListNode newHead = new ListNode(-1);
        ListNode node = newHead;
        for (int i = 0; i < list.size(); i++) {
            node.next = new ListNode(list.get(i));
            node = node.next;
        }
        return newHead.next;
    }
}