您的位置:首页 > 其它

LeetCode--delete/merge sorted list

2015-08-21 14:13 183 查看

Problem I : Remove Duplicates from Sorted List II

Given a sorted linked list, delete all nodes that have duplicate numbers, leaving only distinct numbers from the original list.

删除所有有重复的节点。

For example,

Given 1->2->3->3->4->4->5, return 1->2->5.

Given 1->1->1->2->3, return 2->3.

Tags: Linked List

[code]#include <iostream>

using namespace std;
/*Problem:Remove Duplicates from Sorted List II
Given a sorted linked list, delete all nodes that have duplicate numbers, leaving only distinct numbers from the original list.
*/
struct ListNode{
    int val;
    ListNode *next;
    ListNode(int x):val(x),next(nullptr){}
};
    ListNode* deleteDuplicates(ListNode* head) {
        if(!head||head->next==nullptr)return head;
        ListNode dump(-1);
        ListNode *p_new=&dump;
        ListNode *p=head;
        bool flag=true;
        while(p){
            if(p->next&&p->val==p->next->val){
                ListNode *q=p->next;
                p->next=p->next->next;
                flag=false;
                delete q;
            }
            else {
                if(flag){
                    p_new->next=p;
                    p=p->next;
                    p_new=p_new->next;
                    p_new->next=nullptr;
                }
                else {
                    flag=true;
                    ListNode *q=p;
                    p=p->next;
                    delete q;
                }
            }
        }
        return dump.next;
    }
int main()
{
    ListNode l1(1),l2(3),l3(3),l4(3),l5(5);
    l1.next=&l2;l2.next=&l3;l3.next=&l4;l4.next=&l5;
    ListNode *head=deleteDuplicates(&l1);
    while(head){cout<<head->val<<" ";head=head->next;}
    cout << "Hello world!" << endl;
    return 0;
}


Problem II: Merge k Sorted Lists

Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity.

问题是合并k个排序的链表,合并后的链表保持有序。

分析:第一次使用线性遍历 方式实现合并,结果出现超时测试用例;也算有点纪念意义,代码如下:

[code]    ListNode* mergeKLists(vector<ListNode*>& lists) {
        ListNode dump(-1);
        int n=lists.size();
        if(n==0)return dump.next;
        ListNode *p=&dump;p->next=lists[0];
        for(int i=1;i<n;i++){
            ListNode *head=lists[i];
            ListNode *q=p->next;
            while(q&&head){
                if(q->val<=head->val)
                {
                    p->next=q;
                    p=p->next;
                    q=q->next;
                }
                else{
                    p->next=head;
                    p=p->next;
                    head=head->next;
                }
            }
            if(q){p->next=q;}
            else if(head){p->next=head;}
            p=&dump;
        }
        return dump.next;
    }


分析:使用归并思想。两两合并->最终合为一个链表。我没有看出来这个思路有什么时间上的提升,应该是平均时间复杂度有所提升吧。但看讨论里,发现这个方法的代码能通过测试。代码如下:

[code]#include <iostream>
#include<vector>
using namespace std;
/*Problem:Merge k Sorted Lists
Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity.
*/
struct ListNode{
    int val;
    ListNode *next;
    ListNode(int x):val(x),next(nullptr){}
};
    ListNode* mergelist(ListNode* l1,ListNode* l2){
        if(!l1)return l2;
        if(!l2)return l1;
        if(l1->val<=l2->val){
            l1->next=mergelist(l1->next,l2);
            return l1;
        }
        else{
            l2->next=mergelist(l1,l2->next);
            return l2;
        }
    }
    ListNode* mergeKLists(vector<ListNode*>& lists) {
        //ListNode dump(-1);
        int n=lists.size();
        if(n==0)return nullptr;
        while(n>1){
        for(int i=0;i<n/2;i++){
            lists[i]=mergelist(lists[i],lists[n-1-i]);
        }
        n=(n+1)/2;
        }
        return lists.front();

    }
int main()
{
    ListNode L1_1(1),L1_2(4),L1_3(5),L1_4(9),L1_5(11),L1_6(12);
    L1_1.next=&L1_2;L1_2.next=&L1_3;L1_3.next=&L1_4;L1_4.next=&L1_5;L1_5.next=&L1_6;

    ListNode L2_1(2),L2_2(3),L2_3(4),L2_4(6),L2_5(7),L2_6(10);
    L2_1.next=&L2_2;L2_2.next=&L2_3;L2_3.next=&L2_4;L2_4.next=&L2_5;L2_5.next=&L2_6;

    ListNode L3_1(0),L3_2(2);
    L3_1.next=&L3_2;

    vector<ListNode*> lists={&L1_1,&L2_1,&L3_1};

    ListNode* res=mergeKLists(lists);

    while(res){cout<<res->val<<" ";res=res->next;}

    cout << "Hello world!" << endl;
    return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: