[LeetCode]148.Merge Two Sorted Lists

【题目】

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

【分析】

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

【代码】

/*********************************
*   日期:2015-01-12
*   作者:SJF0115
*   题目: 148.Merge Two Sorted Lists
*   来源: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;
}

时间: 2024-10-16 15:39:30

[LeetCode]148.Merge Two Sorted Lists的相关文章

LeetCode:Merge Two Sorted Lists - 拼接两个有序链表

1.题目名称 Merge Two Sorted Lists(按升序拼接两个有序链表) 2.题目地址 https://leetcode.com/problems/merge-two-sorted-lists/ 3.题目内容 英文:Merge two sorted linked lists and return it as a new list. The new list should be made by splicing together the nodes of the first two l

[LeetCode 题解]: Merge k Sorted Lists

Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity. 题意:对k个有序的链表进行归并排序.并分析其复杂度. /** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x) : val(x), next(N

Java for LeetCode 023 Merge k Sorted Lists

Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity. 解题思路一: 之前我们有mergeTwoLists(ListNode l1, ListNode l2)方法,直接调用的话,需要k-1次调用,每次调用都需要产生一个ListNode[],空间开销很大.如果采用分治的思想,对相邻的两个ListNode进行mergeTwoLists,每次将规模减少一半,直到

[LeetCode] 023. Merge k Sorted Lists (Hard) (C++/Python)

索引:[LeetCode] Leetcode 题解索引 (C++/Java/Python/Sql) Github: https://github.com/illuz/leetcode 023. Merge k Sorted Lists (Hard) 链接: 题目:https://oj.leetcode.com/problems/merge-k-sorted-lists/ 代码(github):https://github.com/illuz/leetcode 题意: 和 021. Merge T

[LeetCode] 23. Merge k Sorted Lists 合并k个有序链表

Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity. 与21. Merge Two Sorted Lists的拓展,这道题要合并k个有序链表,还是可以两两合并. 类似题目: [LeetCode] 21. Merge Two Sorted Lists 合并有序链表 原文地址:https://www.cnblogs.com/lightwindy/p/8512

[LeetCode]21 Merge Two Sorted Lists 合并两个有序链表

---恢复内容开始--- [LeetCode]21 Merge Two Sorted Lists 合并两个有序链表 Description Merge two sorted linked lists and return it as a new list. The new list should be made by splicing together the nodes of the first two lists. Example Example: Input: 1->2->4, 1-&g

[LeetCode][JavaScript]Merge k Sorted Lists

https://leetcode.com/submissions/detail/28015840/ Merge k Sorted Lists Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity. 直接使用了“Merge Two Sorted Lists”里写的方法. http://www.cnblogs.com/Liok3187/p/4514347.ht

【leetcode】Merge k Sorted Lists

Merge k Sorted Lists Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity. 采用优先队列priority_queue 把ListNode放入优先队列中,弹出最小指后,如果该ListNode有下一个元素,则把下一个元素放入到队列中 1 /** 2 * Definition for singly-linked list. 3 * stru

[LeetCode]23. Merge k Sorted Lists

23. Merge k Sorted Lists Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity. 给定k个排序了的链表,合并k个链表成一个排序链表. 本程序思路: 1)首先得到K个链表的长度和存在len中 2)从K个链表中找到值最小的那个节点,把该节点添加到合并链表中 3)重复len次即可把所有节点添加到合并链表中. 注意事项: 1)K个链表中有的