【Leetcode】Sort List解答

一、原题

Sort List

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

二、分析

快速排序和归并排序的时间复杂度是O(nlogn)。如果使用归并排序,在使用链表的情况下,不需要重新申请空间存放排序后的数组,可以做到空间复杂度数O(1)。我在这里使用归并排序来排序链表。

三、代码(java)

<pre name="code" class="java">/**
 * Definition for singly-linked list.
 * class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode(int x) {
 *         val = x;
 *         next = null;
 *     }
 * }
 */
public class Solution {
    public  ListNode sortList(ListNode head){
        if(head==null)
        return null;
	head=mergesort(head);
	return head;
}
public  ListNode  mergesort( ListNode start){
	if(start.next==null)
		return start;
	ListNode p1,p2,pre;
	p1=p2=pre=start;
	while(p1!=null){    //将原链表分成等长的两部分
		if(p1.next!=null){
		p1=p1.next.next;
		pre=p2;
		p2=p2.next;//p2就是中点
		}
		else break;
	}
	pre.next=null;

	start=mergesort(start);
	p2=mergesort(p2);
	start=merge(start,p2);
	return start;
}
public  ListNode merge(ListNode l1,ListNode l2){
	ListNode start,temp,pre;
	pre=new ListNode(0);
	if(l1.val<=l2.val)
		start = l1;
	else
		start = l2;
	temp=null;
	while((l1!=null)&&(l2!=null)){
	 if(l1.val<=l2.val){
		 pre.next=l1;
		 temp=l1.next;
		 l1.next=l2;
		 l1=temp;
         pre=pre.next;
	 }
	 else{
		 pre.next=l2;
		 temp=l2.next;
		 l2.next=l1;
		 l2=temp;
         pre=pre.next;
	 }
	}
    return start;
}
}
				
时间: 2024-08-11 13:07:39

【Leetcode】Sort List解答的相关文章

leetcode Sort Colors

*/--> pre.src {background-color: Black; color: White;} pre.src {background-color: Black; color: White;} pre.src {background-color: Black; color: White;} leetcode Sort Colors 计数排序 注: 题目的要求是将 A 数组重新排列成有序, 而不是将排序的序列输出 Sort Colors Given an array with n o

[leetcode]Sort List @ Python

原题地址:http://oj.leetcode.com/problems/sort-list/ 题意:链表的排序.要求:时间复杂度O(nlogn),空间复杂度O(1). 解题思路:由于题目对时间复杂度和空间复杂度要求比较高,所以查看了各种解法,最好的解法就是归并排序,由于链表在归并操作时并不需要像数组的归并操作那样分配一个临时数组空间,所以这样就是常数空间复杂度了,当然这里不考虑递归所产生的系统调用的栈.   这里涉及到一个链表常用的操作,即快慢指针的技巧.设置slow和fast指针,开始它们都

LeetCode[Sort]: Largest Number

LeetCode[Sort]: Largest Number Given a list of non negative integers, arrange them such that they form the largest number. For example, given [3, 30, 34, 5, 9], the largest formed number is 9534330. Note: The result may be very large, so you need to

[leetcode]Sort Colors @ Python

原题地址:https://oj.leetcode.com/problems/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 integer

LeetCode: 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::Sort List 详细分析

Sort a linked list in O(n log n) time using constant space complexity. 这道题目非常简短的一句话,给链表排序,看到nlogn,我们可以来简单复习一下排序.首先说一下这个nlogn的时间复杂度(根据决策树我们可以得出这个界限),是基于比较排序的最小上限,也就是说,对于没有一定范围情况的数据来说,最快的排序思路就是归并和快速排序了(当然具体的参数系数还是由更具体的设置决定的).对于数组的话,如果使用归并排序,不是in place的

LeetCode: Sort Colors [075]

[题目] 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, a

LeetCode——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] Sort Colors [23]

题目 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

Leetcode:Sort List 对单链表归并排序

Sort a linked list in O(n log n) time using constant space complexity. 看到O(n log n)的排序算法,适合单链表的首先想到的就是归并排序 /** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x) : val(x), next(NULL) {} * }; */ cla