leetcode Sort Listnode

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

findMiddle: 将listnode 不停的拆分

sort: 将拆分好的不同的sort

merge: 将sort好的,向上merge

/**
 * 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||head.next==null){
        	return head;
        }
        ListNode mid=findMiddle(head);

        ListNode right=sortList(mid.next);
        mid.next=null;
        ListNode left=sortList(head);
        return merge(left,right);
    }
    private ListNode findMiddle(ListNode head){
    	ListNode slow=head;
    	ListNode fast=head.next;
    	while(fast!=null&&fast.next!=null){
    		fast=fast.next.next;
    		slow=slow.next;
    	}
    	return slow;
    }
    private ListNode merge(ListNode head1,ListNode head2){
    	ListNode result=new ListNode(0);
    	ListNode temp=result;
    	while(head1!=null&&head2!=null){
    		if(head1.val<=head2.val){
    			temp.next=head1;
    			head1=head1.next;
    		}
    		else{
    			temp.next=head2;
    			head2=head2.next;
    		}
    		temp=temp.next;
    	}
    	if(head1!=null){
    		temp.next=head1;
    	}
    	else{
    		temp.next=head2;
    	}
    	return result.next;
    }
}

  

时间: 2024-11-02 00:01:25

leetcode Sort Listnode的相关文章

[leetcode]Sort List @ Python

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

LeetCode::Sort List 详细分析

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

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

LeetCode: Sort List [148]

[题目] Sort a linked list in O(n log n) time using constant space complexity. [题意] 排序一个链表,要求时间复杂度O(nlogn),使用常量空间 [思路] nlogn的复杂度,用归并排序求解 [代码] /** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x) : v

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]: 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 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