取巧的方法。实际上对链表中val值进行排序然后重新赋值一次。严格来讲没有操作链表指针,没啥技术含量。

import java.util.*;

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

public class Solution {
    /**
     * 
     * @param head ListNode类 
     * @return ListNode类
     */
    public ListNode sortList (ListNode head) {
        // write code here
        if(head == null || head.next == null) return head;
        ListNode cur = head;
        ArrayList<Integer> list = new ArrayList<>();
        while(cur!= null){
            list.add(cur.val);
            cur = cur.next;
        }
        Collections.sort(list);
        cur = head;
        int i = 0;
        while(cur != null){
            cur.val = list.get(i);
            cur = cur.next;
            i++;
        }
        return head;
    }
}