leetcode-排序链表

/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode(int x) { val = x; }
 * }
 */
class Solution {
    public ListNode sortList(ListNode head) {
        PriorityQueue<ListNode> nodequeue = new PriorityQueue<>((o1,o2)->o1.val-o2.val);
        while(head != null){
            ListNode nownode = head;
            head = head.next;
            nownode.next = null;
            nodequeue.add(nownode);
        }
        ListNode retnode = null;
        ListNode temnode = null;
        while(nodequeue.size()>0){
            ListNode addednode = nodequeue.poll();
            if(retnode == null){
                retnode = addednode;
                temnode = retnode;
            }
            else{
                temnode.next = addednode;
                temnode = temnode.next;
            }
        }
        return retnode;
    }
}

排序链表,我的做法是把链表切开,然后拿优先队列去装,然后再重新拼接

发布了48 篇原创文章 · 获赞 0 · 访问量 4326

猜你喜欢

转载自blog.csdn.net/weixin_41327340/article/details/103788162
今日推荐