leetcode - [4]Sort List

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

思路:采用归并排序或者快速排序

#include <iostream>
using namespace std;

struct ListNode {
    int val;
    ListNode *next;
    ListNode(int x): val(x), next(NULL) {}
};

class Solution {
public:
    ListNode *sortList(ListNode *head) {
        if (!head || head->next == NULL) return head;

        ListNode *PA = head, *PB = head;
        while (PA && PB && PB->next && PB->next->next) {
            PA = PA->next;
            PB = PB->next->next;
        }
        PB = PA->next;
        PA->next = NULL;
        PA = head;

        PA = sortList(PA);
        PB = sortList(PB);
        return mergeList(PA, PB);
    }

    ListNode *mergeList(ListNode *PA, ListNode *PB) {
        if (!PB) return PA;
        if (!PA) return PB;

        ListNode *merge, *head;
        if (PA->val < PB->val) {
            head = PA;
            PA = PA->next;
        }
        else {
            head = PB;
            PB = PB->next;
        }

        merge = head;
        while (PA && PB) {
            if (PA->val < PB->val) {
                merge->next = PA;
                PA = PA->next;
            }
            else {
                merge->next = PB;
                PB = PB->next;
            }
            merge = merge->next;
        }

        if (PA) merge->next = PA;
        if (PB) merge->next = PB;

        return head;
    }
};

int main(int argc ,char *argv[])
{
    ListNode *p, *q;
    p = new ListNode(3);
    p->next = new ListNode(2);
    p->next->next = new ListNode(4);

    Solution *solution = new Solution();

    p = solution->sortList(p);
    while(p) {
        cout << p->val << endl;
        p = p->next;
    }
    return 0;
}
时间: 2024-10-12 23:23:46

leetcode - [4]Sort List的相关文章

LeetCode Insertion Sort List

class Solution { public: ListNode *insertionSortList(ListNode *head) { if (head == NULL) return NULL; ListNode* sorted_head = head; ListNode* unsorted_head = head->next; head->next = NULL; ListNode* cur = unsorted_head; while (cur != NULL) { unsorte

【LeetCode】Sort Colors

LeetCode OJ 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, w

[leetcode]Insertion Sort List @ Python

原题地址:http://oj.leetcode.com/problems/insertion-sort-list/ 题意:对链表进行插入排序. 解题思路:首先来对插入排序有一个直观的认识,来自维基百科. 代码循环部分图示: 代码: class Solution: # @param head, a ListNode # @return a ListNode def insertionSortList(self, head): if not head: return head dummy = Lis

leetcode——Insertion Sort List 对链表进行插入排序(AC)

Sort a linked list using insertion sort. class Solution { public: ListNode *insertionSortList(ListNode *head) { if(head == NULL || head->next == NULL) return head; ListNode *result; result->val = INT_MIN; result->next = NULL; ListNode *cur=head,*

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

Sort a linked list using insertion sort. 仍然是一个非常简洁的题目,让我们用插入排序给链表排序:这里说到插入排序,可以来回顾一下, 最基本的入门排序算法,就是插入排序了:时间复杂度为n^2,最基本的插入排序是基于数组实现的,下面给出基于数组实现的插入排序,来体会一个插入排序的思想: 以下仅为数组实现,不是解题代码,没兴趣可以跳过. void insertionsort (int a[], int N) { for (int i = 1; i < N; i+

leetcode 专题—sort

此将主要将leetcode中sort专题的解答都放在这里,后续会慢慢加入 一:leetcode179 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 re

LeetCode OJ - Sort List

题目: Sort a linked list in O(n log n) time using constant space complexity. 解题思路: 复杂度为O(n* logn) 的排序算法有:快速排序.堆排序.归并排序.对于链表这种数据结构,使用归并排序比较靠谱.递归代码如下: 代码: /** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNod

LeetCode: Insertion Sort List [147]

[题目] Sort a linked list using insertion sort. [题意] 用插入排序方法排序链表 [思路] 直接搞 [代码] /** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x) : val(x), next(NULL) {} * }; */ class Solution { public: ListNode

leetcode linkedList sort

链表排序:算法-归并排序 public class LinkedSort { private static class ListNode { int val; ListNode next; ListNode(int x) { val = x; next = null; } } private ListNode mergeList(ListNode head1,ListNode head2){ if(head1==null){ return head2; }else if(head2==null)