[LeetCode]148.Sort List

简介:

【题目】

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

【分析】

单链表适合用归并排序,双向链表适合用快速排序。本题可以复用Merge Two Sorted Lists方法

【代码】

/*********************************
*   日期:2015-01-12
*   作者:SJF0115
*   题目: 148.Sort List
*   来源:https://oj.leetcode.com/problems/sort-list/
*   结果:AC
*   来源:LeetCode
*   博客:
**********************************/
#include <iostream>
#include <climits>
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 == NULL || head->next == NULL){
            return head;
        }//if
        // 定义两个指针
        ListNode *fast = head,*slow = head;
        // 慢指针找到中间节点
        while(fast->next != NULL && fast->next->next != NULL){
            // 快指针一次两步
            fast = fast->next->next;
            // 慢指针一次一步
            slow = slow->next;
        }//while
        // 从中间节点断开
        fast = slow->next;
        slow->next = NULL;
        // 前段排序
        ListNode *left = sortList(head);
        // 后段排序
        ListNode *right = sortList(fast);
        // 前后段合并
        return mergeTwoLists(left,right);
    }
private:
    ListNode *mergeTwoLists(ListNode *l1, ListNode *l2) {
        ListNode *head = new ListNode(-1);
        for(ListNode *p = head;l1 != NULL || l2 != NULL;p = p->next){
            int val1 = (l1 == NULL) ? INT_MAX : l1->val;
            int val2 = (l2 == NULL) ? INT_MAX : l2->val;
            if(val1 <= val2){
                p->next = l1;
                l1 = l1->next;
            }//if
            else{
                p->next = l2;
                l2 = l2->next;
            }//else
        }//for
        return head->next;
    }
};

int main(){
    Solution solution;
    int A[] = {8,3,10,5,11,1,4};
    // 链表
    ListNode *head = new ListNode(A[0]);
    ListNode *p = head;
    for(int i = 1;i < 7;i++){
        ListNode *node = new ListNode(A[i]);
        p->next = node;
        p = node;
    }//for
    head = solution.sortList(head);
    // 输出
    p = head;
    while(p){
        cout<<p->val<<" ";
        p = p->next;
    }//while
    cout<<endl;
    return 0;
}


目录
相关文章
|
6月前
|
Java
Leetcode 114. Flatten Binary Tree to Linked List
思路也很简单,先把root的左子树(如有)变成单链表 leftlinkedlist,把root的右子树(如有)变成单链表 rightlinkedlist,再把root的右节点变成leftlikedlist,再把rightlinkedlist接到leftlinkedlist后面,代码如下。
21 1
|
6月前
|
C++
Leetcode Copy List with Random Pointer(面试题推荐)
给大家推荐一道leetcode上的面试题,这道题的具体讲解在《剑指offer》的P149页有思路讲解,如果你手头有这本书,建议翻阅。
29 0
|
6月前
Leetcode 19.Remove Nth Node From End of List
删除单链表中的倒数第n个节点,链表中删除节点很简单,但这道题你得先知道要删除哪个节点。在我的解法中,我先采用计数的方式来确定删除第几个节点。另外我在头节点之前额外加了一个节点,这样是为了把删除头节点的特殊情况转换为一般情况,代码如下。
21 0
|
5天前
|
大数据 Java 程序员
「LeetCode合集」链表(List)及经典问题
「LeetCode合集」链表(List)及经典问题
29 0
LeetCode 75 Sort Colors 颜色分类(荷兰国旗)
Given an array with n objects colored red, white or blue, sort them in-place 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 blue respectively.
70 0
LeetCode 141. 环形链表 Linked List Cycle
LeetCode 141. 环形链表 Linked List Cycle
LeetCode 234. 回文链表 Palindrome Linked List
LeetCode 234. 回文链表 Palindrome Linked List
LeetCode 206. 反转链表 Reverse Linked List
LeetCode 206. 反转链表 Reverse Linked List
LeetCode 19. 删除链表的倒数第N个节点 Remove Nth Node From End of List
LeetCode 19. 删除链表的倒数第N个节点 Remove Nth Node From End of List
LeetCode 237. 删除链表中的节点 Delete Node in a Linked List
LeetCode 237. 删除链表中的节点 Delete Node in a Linked List