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)