leetcode 21|23. Merge Two|k Sorted Lists

来源:互联网 发布:nerf淘宝 编辑:程序博客网 时间:2024/06/06 06:31

21. Merge Two Sorted Lists

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.

/** * Definition for singly-linked list. * struct ListNode { *     int val; *     ListNode *next; *     ListNode(int x) : val(x), next(NULL) {} * }; */class Solution {public:    ListNode* mergeTwoLists(ListNode* p1, ListNode* p2)     {        //way-1        /*        if(p1==NULL)            return p2;        if(p2==NULL)            return p1;        ListNode *result,*middle,*q,*nex;             if(p1->val<p2->val)        {            result=p1;            q=p1;            middle=p2;        }        else        {            result=p2;             q=p2;            middle=p1;        }                 while(q->next)        {            if(q->next->val<=middle->val)            {                q=q->next;            }            else            {                nex=q->next;                q->next=middle;                q=middle;                middle=nex;            }        }        q->next=middle;        return result;        */                //way-2        if(p1==NULL)            return p2;        if(p2==NULL)            return p1;        multimap<int,ListNode*> mm;                mm.insert(pair<int,ListNode*>(p1->val,p1));        mm.insert(pair<int,ListNode*>(p2->val,p2));                ListNode head(-1);        ListNode *p=&head;                while(!mm.empty())        {            auto it=mm.begin();            p->next=it->second;            p=p->next;            ListNode *q=it->second->next;            if(q!=NULL)                mm.insert(pair<int,ListNode*>(q->val,q));            mm.erase(it);        }        return head.next;            }};




23. Merge k Sorted Lists

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

way-1:分治算法
每次从数组中取两个链表,将合并结果加入到链表中,反复这个过程,直到数组中只剩一个链表为止,对两个链表进行合并的代码可以复用LeetCode21的代码。

/** * Definition for singly-linked list. * struct ListNode { *     int val; *     ListNode *next; *     ListNode(int x) : val(x), next(NULL) {} * }; */class Solution {public:ListNode* merge2Lists(ListNode* a,ListNode* b){    ListNode head(-1);    head.next=(a->val > b->val)?b:a;    ListNode * LastPoint=head.next;    ListNode * WillIn=(a->val > b->val)?a:b;      while(1)    {        if(!LastPoint->next)        {            LastPoint->next=WillIn;            return head.next;        }        else if(!WillIn)            return head.next;        else        {            if(LastPoint->next->val > WillIn->val)            {                ListNode * temp=LastPoint->next;                LastPoint->next=WillIn;                WillIn=temp;                LastPoint=LastPoint->next;            }            else            {                LastPoint=LastPoint->next;            }        }    }    return head.next;}ListNode* mergeKLists(vector<ListNode*>& lists){if (lists.size() == 0)return NULL;while(lists.size()>1){    if(lists[0]==NULL)//erase null list            {                lists.erase(lists.begin());                continue;            }            else if(lists[1]==NULL)            {                lists.erase(lists.begin()+1);                continue;            }    else    {        lists[0]=merge2Lists(lists[0],lists[1]);        lists.erase(lists.begin()+1);    }}return lists[0];}};



way-2:把每个链表的头一个存在map里,利用map的排序功能,每拿出最小的一个,把跟着的一个补进去

/** * Definition for singly-linked list. * struct ListNode { *     int val; *     ListNode *next; *     ListNode(int x) : val(x), next(NULL) {} * }; */class Solution {public:    ListNode* mergeKLists(vector<ListNode*>& lists)    {        multimap<int, ListNode*> mm;        for(int i=0;i<lists.size();i++)        {            if(lists[i]==NULL)            {                lists.erase(lists.begin()+i);                i--;                continue;            }            else                mm.insert(pair<int,ListNode*>(lists[i]->val, lists[i]));        }                ListNode head(-1);        ListNode *p=&head;                while(!mm.empty())        {            auto it = mm.begin();            p->next = it->second;            p = p->next;            ListNode *q = it->second->next;            if(q != NULL)                mm.insert(pair<int,ListNode*>(q->val, q));            mm.erase(it);        }        return head.next;    }};






原创粉丝点击