Sort List leetcode

实现单链表排序   时间复杂度要求为   nlogn

由于是单链表,用快速排序无法往前面遍历(双向链表可以考虑),这里我们用到归并排序

代码如下:

<span style="font-size:18px;">/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode(int x) : val(x), next(NULL) {}
 * };
 */
class Solution {
public:
    ListNode *sortList(ListNode *head) {

        if(head==NULL||head->next==NULL)
            return head;

        ListNode *onestep=head;
        ListNode *twostep=head;//定义两个指针,一个走一步,一个走两步,用来找到中间节点

       // ListNode *first=NULL;
       // ListNode *second=NULL;

        while(twostep->next!=NULL&&twostep->next->next!=NULL)
        {
            onestep=onestep->next;
            twostep=twostep->next->next;
        }
        //此时onestep指向了整个链表的中间,如果偶数那么两边均衡,如果为奇数,指向正中间需要从onestep出分开

        twostep=onestep;
        onestep=onestep->next;//此时onestep指向后半部分

        twostep->next=NULL;//将前半部分和后半部分分开

        twostep=sortList(head);
        onestep=sortList(onestep);

        return meger(twostep,onestep);

    }
    ListNode *meger(ListNode *first,ListNode *second)
    {
        ListNode *result;

        ListNode *p;

        if(first==NULL)
            return second;
        if(second==NULL)
            return first;

       //初始化result

        if(first->val<second->val){
            result=first;
            first=first->next;
        }
        else
        {
            result=second;
            second=second->next;
        }
        p=result;

        while(first!=NULL&&second!=NULL)
        {
            if(first->val<second->val)
            {
                p->next=first;
                first=first->next;
            }
            else
            {
                p->next=second;
                second=second->next;
            }
            p=p->next;
        }
        if(first!=NULL)
            p->next=first;
       else if(second!=NULL)
            p->next=second;

        return result;
    }

};</span>
时间: 2024-10-11 05:26:29

Sort List leetcode的相关文章

Sort Colors leetcode java

题目: 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, an

Sort List leetcode java

题目: Sort a linked list in O(n log n) time using constant space complexity. 题解: 考虑到要求用O(nlogn)的时间复杂度和constant space complexity来sort list,自然而然想到了merge sort方法.同时我们还已经做过了merge k sorted list和merge 2 sorted list.这样这个问题就比较容易了. 不过这道题要找linkedlist中点,那当然就要用最经典的

sort学习 - LeetCode #406 Queue Reconstruction by Height

用python实现多级排序,可以像C语言那样写个my_cmp,然后在sort的时候赋给参数cmp即可 但实际上,python处理cmp 是很慢的,因为每次比较都会调用my_cmp:而使用key和reverse就快得多,因为只需要在排序开始时处理一次即可,因此在排序的时候能不用cmp就尽量不用 另外可以用operator函数中的itemgetter,和attrgetter实现基于key的(多级)排序: from operator import itemgetter, attrgetter sort

Sort Colors -- leetcode

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

Insertion Sort List —— LeetCode

Sort a linked list using insertion sort. 题目大意:将一个单链表使用插入排序的方式排序. 解题思路:先新建一个头指针,然后重新构建一下这个单链表,每次从头找到第一个比当前元素大的,插在这个元素前面. /** * Definition for singly-linked list. * public class ListNode { * int val; * ListNode next; * ListNode(int x) { val = x; } * }

排序算法之快速排序(Quick Sort) -- 适用于Leetcode 75 Sort Colors

Quick Sort使用了Divide and Concur的思想: 找一个基准数, 把小于基准数的数都放到基准数之前, 把大于基准数的数都放到基准数之后 Worst case: O(n^2) Average case: O(nlogN) 步骤: 初始的数组 Array a[]: 0 1 2 3 4 5 6 7 8 9 51 73 52 18 91 7 87 73 48 3 基准数: X = a[0] = 51 i 的值: i = 0 j 的值: j = 9 (a.length) Step 1:

Sort List[leetcode] 由归并排序的递归和循环,到本题的两种解法

归并排序可以有两种思路----top-down 和 bottom-up top-down: 递归实现,将数组分成两半,分别处理:再合并. 伪代码如下: split ( A[], l, r) { if ( r - l < 2) return; m = (r + l) / 2; split ( A, l, m); //split A[l-m-1] split ( A, m, r); //split A[m-r-1] merge ( A, l, m, e); //merge A[l-m-1] and

Sort Colors —— LeetCode

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

Sort List ——LeetCode

Sort a linked list in O(n log n) time using constant space complexity. 链表排序,要求时间复杂度O(nlgn),我写的归并排序. /** * Definition for singly-linked list. * public class ListNode { * int val; * ListNode next; * ListNode(int x) { val = x; } * } */ public class Solu

791. Custom Sort String - LeetCode

Question 791. Custom Sort String Solution 题目大意:给你字符的顺序,让你排序另一个字符串. 思路: 输入参数如下: S = "cba" T = "abcd" 先构造一个map,sMap key存储S中出现的字符,value存储字符在S中的位置 c -> 0 b -> 1 a -> 2 再构造一个int数组,sIdx sIdx,存储S中的字符在T字符串中出现的次数 遍历T字符串 如果字符在sMap中,sIdx