Java for LeetCode 148 Sort List

Sort a linked list in O(n log n) time using constant space complexity.

解题思路:

归并排序、快速排序、堆排序都是O(n log n),由于优先级队列是用堆排序实现的,因此,我们使用优先级队列即可,JAVA实现如下:

    public ListNode sortList(ListNode head) {
    	if(head==null||head.next==null)
    		return head;
        Queue<ListNode> pQueue = new PriorityQueue<ListNode>(
                0,new Comparator<ListNode>() {//提交的时候不能加初始容量(第一个参数),否则报错
                    public int compare(ListNode l1, ListNode l2) {
                        return l1.val - l2.val;
                    }
                });
        while(head!=null){
        	pQueue.add(head);
        	head=head.next;
        }
        head=pQueue.poll();
        ListNode temp=head;
        while(pQueue.size()>0){
        	temp.next=pQueue.poll();
        	temp=temp.next;
        }
        temp.next=null;
        return head;
    }
时间: 2024-12-06 16:42:14

Java for LeetCode 148 Sort List的相关文章

Leetcode 148. Sort List 归并排序 in Java

148. Sort List Total Accepted: 81218 Total Submissions: 309907 Difficulty: Medium Sort a linked list in O(n log n) time using constant space complexity. /** * Definition for singly-linked list. * public class ListNode { * int val; * ListNode next; *

[Lintcode]98. Sort List/[Leetcode]148. Sort List

98. Sort List/148. Sort List 本题难度: Medium Topic: Linked List Description Sort a linked list in O(n log n) time using constant space complexity. Example Example 1: Input: 1->3->2->null Output: 1->2->3->null Example 2: Input: 1->7->2

leetcode 148. Sort List ----- java

Sort a linked list in O(n log n) time using constant space complexity. 排序,要求是O(nlog(n))的时间复杂度和常数的空间复杂度,那么就使用归并就可以了. /** * Definition for singly-linked list. * public class ListNode { * int val; * ListNode next; * ListNode(int x) { val = x; } * } */ p

[LeetCode] 148. Sort List 解题思路

Sort a linked list in O(n log n) time using constant space complexity. 问题:对一个单列表排序,要求时间复杂度为 O(n*logn),额外空间为 O(1). O(n*logn) 时间排序算法,无法是 quick sort, merge sort, head sort.quick sort 需要灵活访问前后元素,适合于数组,merge sort 只需要从左到右扫过去即可,可用于列表结构. 当列表元素个数大于2时,将列表拆分为左右

Java for LeetCode 075 Sort Colors

Given an array with n objects colored red, white or blue, sort them so that objects of the same color are adjacent, with the colors in the order red, white and blue. Here, we will use the integers 0, 1, and 2 to represent the color red, white, and bl

[LeetCode] 148. Sort List 链表排序

Sort a linked list in O(n log n) time using constant space complexity. Example 1: Input: 4->2->1->3 Output: 1->2->3->4 Example 2: Input: -1->5->3->4->0 Output: -1->0->3->4->5 解法:归并排序.由于有时间和空间复杂度的要求.把链表从中间分开,递归下去,都

[LeetCode#148]Sort List

The problem: Sort a linked list in O(n log n) time using constant space complexity. My analysis: The idea behind this problem is easy : merge sort !But we should learn some tricky skills from this question. 1. How to split a linked list into two sepa

[LeetCode]148. Sort List链表归并排序

要求时间复杂度O(nlogn),空间复杂度O(1),采用归并排序 传统的归并排序空间复杂度是O(n),原因是要用一个数组表示合并后的数组,但是这里用链表表示有序链表合并后的链表,由于链表空间复杂度是O(1),所以可以. 链表问题经常出现TLE问题或者MLE问题,这时候要检查链表拼接过程或者循环过程,看有没有死循环 public ListNode sortList(ListNode head) { if (head==null||head.next==null) return head; //利用

Java for LeetCode 057 Insert Interval

Given a set of non-overlapping intervals, insert a new interval into the intervals (merge if necessary). You may assume that the intervals were initially sorted according to their start times. Example 1: Given intervals [1,3],[6,9], insert and merge