C++s merge two linked list (Ox-Guest sword refers to offer)

Source: Internet
Author: User

This code can not bear to look directly, do not think so in the ox-Guest sword refers to the offer to pass. #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);p 1->next = P2;p2->next = P3;show (p1); ListNode *P4 = new ListNode (2); ListNode *p5 = new ListNode (4); ListNode *p6 = new ListNode (6);p 4->next = P5;p5->next = P6;show (p4); Solution SL; listnode* n = sl. Merge (P1,P4); show (n); return 0;}

C++s merge two linked list (Ox-Guest sword refers to offer)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.