
1 /*
2 struct ListNode {
3 int val;
4 struct ListNode *next;
5 ListNode(int x) :
6 val(x), next(NULL) {
7 }
8 };*/
9 class Solution {
10 public:
11 ListNode* Merge(ListNode* pHead1, ListNode* pHead2)
12 {
13 ListNode* newhead = new ListNode(-1);
14 ListNode* temp = newhead;
15 if(!pHead1) return pHead2;
16 if(!pHead2) return pHead1;
17 while(pHead1 && pHead2){
18 if(pHead1->val <=pHead2->val){
19 temp->next = pHead1;
20 pHead1 = pHead1->next;
21 temp = temp->next;
22 }
23 else{
24 temp->next = pHead2;
25 pHead2 = pHead2->next;
26 temp = temp->next;
27 }
28 }
29 if(!pHead1){
30 temp->next = pHead2;
31 }
32 if(!pHead2){
33 temp->next = pHead1;
34 }
35 return newhead->next;
36 }
37 };
1 /*
2 struct ListNode {
3 int val;
4 struct ListNode *next;
5 ListNode(int x) :
6 val(x), next(NULL) {
7 }
8 };*/
9 class Solution {
10 public:
11 ListNode* Merge(ListNode* pHead1, ListNode* pHead2)
12 {
13 ListNode* head;
14 if(!pHead1) return pHead2;
15 if(!pHead2) return pHead1;
16 if(pHead1->val <= pHead2->val){
17 head=pHead1;
18 head->next = Merge(pHead1->next,pHead2);
19 }
20 else{
21 head = pHead2;
22 head->next = Merge(pHead1,pHead2->next);
23 }
24 return head;
25 }
26 };