C++s合并兩個鏈表(牛客劍指offer)

來源:互聯網
上載者:User

標籤:

/////這段代碼不忍直視,想不通這樣在牛客劍指offer上都可以通過.。#include <iostream>using namespace std;struct ListNode{int val;struct ListNode *next;ListNode(int x):val(x),next(NULL){}};void show(ListNode *root);class Solution {public:ListNode* Merge(ListNode* pHead1, ListNode* pHead2){                ListNode *root=NULL;        ListNode *p = pHead1;        ListNode *q = pHead2;        ListNode *m = NULL;        ListNode *n = NULL;        if(p==NULL)return q;        if(q==NULL)return p;        while(p!=NULL && q!=NULL)        {                        if(p->val>q->val)            {                n=q->next;                if(root==NULL)                {                root = new ListNode(q->val);                }                else                {                    ListNode *k = root;//m=NULL;                    ListNode *s = new ListNode(q->val);                    while(k!=NULL)                    {                        m=k;                        k=k->next;                    }                    if(m!=NULL)                    {                        m->next=s;//m=s;                    }                }                delete q;                q=n;            }            else            {                n=p->next;                if(root==NULL)                {                    root = new ListNode(p->val);                }                else                {//m=NULL;                    ListNode *k = root;                    ListNode *s = new ListNode(p->val);                    while(k!=NULL)                    {                        m=k;                        k=k->next;                    }                    if(m!=NULL)                    {                        m->next=s;//m=s;                    }                }                delete p;                p=n;            }        }m=root;        if(q==NULL)        {          while(m!=NULL){n=m;m=m->next;}n->next=p;          }        if(p==NULL)        {            while(m!=NULL){n=m;m=m->next;}n->next=q;        }   //show(root);return root;       }};void show(ListNode *root){ListNode *p = root;while(p!=NULL){cout<<p->val<<"  ";p=p->next;}cout<<endl;}int main(){ListNode *p1 = new ListNode(1);ListNode *p2 = new ListNode(3);ListNode *p3 = new ListNode(5);p1->next = p2;p2->next = p3;show(p1);ListNode *p4 = new ListNode(2);ListNode *p5 = new ListNode(4);ListNode *p6 = new ListNode(6);p4->next = p5;p5->next = p6;show(p4);Solution sl;ListNode* n = sl.Merge(p1,p4);show(n);return 0;}

C++s合并兩個鏈表(牛客劍指offer)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.