-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathquestion25.cpp
More file actions
43 lines (39 loc) · 846 Bytes
/
Copy pathquestion25.cpp
File metadata and controls
43 lines (39 loc) · 846 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
/*
输入两个单调递增的链表,输出两个链表合成后的链表,当然我们需要合成后的链表满足单调不减规则。
Xiaobin Tian;
*/
#include<vector>
#include<stack>
#include<stdexcept>
using namespace::std;
struct ListNode {
int val;
ListNode *next;
ListNode(int x) : val(x), next(nullptr) {}
};
class Solution {
public:
ListNode* Merge(ListNode* pHead1, ListNode* pHead2){
ListNode *dummyhead = new ListNode(0);
ListNode *r = dummyhead;
ListNode *p = pHead1, *q = pHead2;
while(p != nullptr && q != nullptr){
if(p->val < q->val){
r->next = p;
p = p->next;
r = r->next;
}
else{
r->next = q;
q = q->next;
r = r->next;
}
}
if(p == nullptr)
r->next = q;
if(q == nullptr)
r->next = p;
return dummyhead->next;
}
};