剑指offer-算法题练习:part18 合并两个排序的链表

来源:互联网 发布:美国非农数据11月预测 编辑:程序博客网 时间:2024/05/21 15:09

剑指offer-算法题练习:part18 合并两个排序的链表

时间限制:1秒空间限制:32768K
本题知识点: 链表

题目描述

输入两个单调递增的链表,输出两个链表合成后的链表,当然我们需要合成后的链表满足单调不减规则。

code1——非递归,比较大小

/*struct ListNode {int val;struct ListNode *next;ListNode(int x) :val(x), next(NULL) {}};*/class Solution {public:    ListNode* Merge(ListNode* pHead1, ListNode* pHead2)    {        ListNode* head;        ListNode* r;//result        if(pHead1==NULL) return pHead2;        if(pHead2==NULL) return pHead1;        //取较小的值作为头结点        if(pHead1->val<=pHead2->val){            head=pHead1;            pHead1=pHead1->next;        }else{            head=pHead2;            pHead2=pHead2->next;        }        r=head;//保存头结点到r,        //<span style="color:#ff0000;">与part17 反转链表code2的方法类似</span>,保存头结点很重要,        //如果下面的while循环是用r则return head,如果用head则return r        while(pHead1!=NULL&&pHead2!=NULL){            if(pHead1->val<=pHead2->val){                head->next = pHead1;                pHead1 = pHead1->next;                head = head->next;            }//if            else{                head->next = pHead2;                pHead2 = pHead2->next;                head = head->next;            }//else        }//while        if(pHead1 == NULL) head->next = pHead2;        if(pHead2 == NULL) head->next =pHead1;        return r;    }};
r=head;这句如果在后面的while中使用r进行链表存储,则return head;代码如下:

/*struct ListNode {int val;struct ListNode *next;ListNode(int x) :val(x), next(NULL) {}};*/class Solution {public:    ListNode* Merge(ListNode* pHead1, ListNode* pHead2)    {        ListNode* head;        ListNode* r;//result        if(pHead1==NULL) return pHead2;        if(pHead2==NULL) return pHead1;        //取较小的值作为头结点        if(pHead1->val<=pHead2->val){            head=pHead1;            pHead1=pHead1->next;        }else{            head=pHead2;            pHead2=pHead2->next;        }        r=head;//保存头结点到r,        //与part17 反转链表code2的方法类似,保存头结点很重要,        //如果下面的while循环是用r则return head        while(pHead1!=NULL&&pHead2!=NULL){            if(pHead1->val<=pHead2->val){                r->next = pHead1;                pHead1 = pHead1->next;                r = r->next;            }//if            else{                r->next = pHead2;                pHead2 = pHead2->next;                r = r->next;            }//else        }//while        if(pHead1 == NULL) r->next = pHead2;        if(pHead2 == NULL) r->next =pHead1;        return head;    }};

code2——递归算法

/*struct ListNode {int val;struct ListNode *next;ListNode(int x) :val(x), next(NULL) {}};*/class Solution {public:    ListNode* Merge(ListNode* pHead1, ListNode* pHead2)    {        ListNode* p=NULL;        //ListNode* r;        if(pHead1==NULL) return pHead2;        if(pHead2==NULL) return pHead1;        if(pHead1->val <= pHead2->val){            p = pHead1;//            p->next = Merge(pHead1->next,pHead2);        }        else{            p = pHead2;            p->next = Merge(pHead1,pHead2->next);        }        return p;    }};

以下代码也可通过:

/*struct ListNode {int val;struct ListNode *next;ListNode(int x) :val(x), next(NULL) {}};*/class Solution {public:    ListNode* Merge(ListNode* pHead1, ListNode* pHead2)    {        ListNode* p=NULL;        //ListNode* r;        if(pHead1==NULL) return pHead2;        if(pHead2==NULL) return pHead1;        if(pHead1->val <= pHead2->val){            //p = pHead1;//            pHead1->next = Merge(pHead1->next,pHead2);            return pHead1;        }        else{            //p = pHead2;            pHead2->next = Merge(pHead1,pHead2->next);            return pHead2;        }        //return p;    }};



0 0
原创粉丝点击