题解 | #单链表的排序#
单链表的排序
https://www.nowcoder.com/practice/f23604257af94d939848729b1a5cda08
	方法1:把值传进来,只对值进行排序,然后将排序后的值依次赋给节点
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) {
        if(head==null)return null;
        ArrayList<Integer> temp=new ArrayList<>();
        ListNode cur=head;
        while(cur!=null){
            temp.add(cur.val);
            cur=cur.next;
        }
        temp.sort((a,b)->{return a-b;});
        cur=head;
        for(Integer valI:temp){
            cur.val=valI;
            cur=cur.next;
        }
        temp.clear();
        return head;
    
    }
    
}
 腾讯云智研发成长空间 216人发布
腾讯云智研发成长空间 216人发布 查看11道真题和解析
查看11道真题和解析