LeetCode21. Merge Two Sorted Lists

来源:互联网 发布:重装系统保留软件 编辑:程序博客网 时间:2024/05/17 04:42

1、去重了,所以没通过

#include<cstdlib>#include<iostream>using namespace std;struct ListNode {    int val;    ListNode *next;    ListNode(int x) : val(x), next(NULL) {}};class Solution {public:    ListNode* mergeTwoLists(ListNode* l1, ListNode* l2) {        if(l1==NULL&&l2==NULL) return l1;        if(l1==NULL) return l2;        if(l2==NULL) return l1;        ListNode *h,*p,*temp,*p1,*p2;        if((*l1).val<(*l2).val){            h = l1;            p1= l1->next;            p =h;            p2=l2;        }        else if((*l1).val==(*l2).val){            h = l1;            p1= l1->next;            p =h;            p2= l2->next;        }        else{            p2=l1;            h =l2;            p =h;            p1= l2->next;        }         while((p1!=NULL)&&(p2!=NULL)){            if((*p1).val<(*p2).val){                p->next=p1;                p1=p1->next;            }            else if((*p1).val==(*p2).val){                p->next=p1;                p1=p1->next;                p2=p2->next;            }            else{                temp=p1;                p1=p2;                p2=temp;                p->next=p1;                p1=p1->next;            }               p=p->next;        }        if(p1!=NULL){             p->next=p1;        }        else {             p->next=p2;        }        return h;    }};void main(){    ListNode L1=ListNode(1);    ListNode L2=ListNode(2);    ListNode L3=ListNode(5);    ListNode L4=ListNode(8);    ListNode L9=ListNode(9);    L1.next=&L2;    L2.next=&L3;    L3.next=&L4;    L4.next=&L9;    ListNode *head1=&L1;    ListNode *p=head1;    ListNode L5=ListNode(2);    ListNode L6=ListNode(3);    ListNode L7=ListNode(4);    ListNode L8=ListNode(7);    L5.next=&L6;    L6.next=&L7;    L7.next=&L8;    ListNode *head2=&L5;    ListNode *p2=head2;    Solution So;    p=So.mergeTwoLists(head1,head2);    while(p!=NULL)    {        cout<<(*p).val<<endl;        p=p->next;      }}

2、19ms,太慢了

#include<cstdlib>#include<iostream>using namespace std;struct ListNode {    int val;    ListNode *next;    ListNode(int x) : val(x), next(NULL) {}};class Solution {public:    ListNode* mergeTwoLists(ListNode* l1, ListNode* l2) {        if(l1==NULL&&l2==NULL) return l1;        if(l1==NULL) return l2;        if(l2==NULL) return l1;        ListNode *h,*p,*temp,*p1,*p2;        if((*l1).val<=(*l2).val){            h = l1;            p1= l1->next;            p =h;            p2=l2;        }        else{            p2=l1;            h =l2;            p =h;            p1= l2->next;        }         while((p1!=NULL)&&(p2!=NULL)){            if((*p1).val<=(*p2).val){                p->next=p1;                p1=p1->next;            }            else{                temp=p1;                p1=p2;                p2=temp;                p->next=p1;                p1=p1->next;            }               p=p->next;        }        if(p1!=NULL){             p->next=p1;        }        else {             p->next=p2;        }        return h;    }};void main(){    ListNode L1=ListNode(1);    ListNode L2=ListNode(2);    ListNode L3=ListNode(5);    ListNode L4=ListNode(8);    ListNode L9=ListNode(9);    L1.next=&L2;    L2.next=&L3;    L3.next=&L4;    L4.next=&L9;    ListNode *head1=&L1;    ListNode *p=head1;    ListNode L5=ListNode(2);    ListNode L6=ListNode(3);    ListNode L7=ListNode(4);    ListNode L8=ListNode(7);    L5.next=&L6;    L6.next=&L7;    L7.next=&L8;    ListNode *head2=&L5;    ListNode *p2=head2;    Solution So;    p=So.mergeTwoLists(head1,head2);    while(p!=NULL)    {        cout<<(*p).val<<endl;        p=p->next;      }}

3、12 ms

class Solution {public:    ListNode* mergeTwoLists(ListNode* l1, ListNode* l2) {        if(l1==NULL&&l2==NULL) return l1;        if(l1==NULL) return l2;        if(l2==NULL) return l1;        ListNode head(-1);        ListNode* temp=&head;         while(l1&&l2){            if(l1->val<=l2->val){                temp->next=l1;                l1=l1->next;                        }            else{                temp->next=l2;                l2=l2->next;                        }               temp=temp->next;        }        if(l1){             temp->next=l1;        }        else {             temp->next=l2;        }        return head.next;    }};

4、递归解法 13ms

class Solution {public:    ListNode* mergeTwoLists(ListNode* l1, ListNode* l2) {        if(l1==NULL) return l2;        if(l2==NULL) return l1;        if(l1->val<=l2->val){            l1->next=mergeTwoLists(l1->next,l2);            return l1;                  }        else{            l2->next=mergeTwoLists(l2->next,l1);            return l2;                  }       }};
0 0
原创粉丝点击