题目描述

合并两个升序链表,并返回.

思路

直接偷懒了…

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

class Solution {
public:
ListNode* mergeTwoLists(ListNode* list1, ListNode* list2) {
if(!list1)return list2;
else if (!list2)return list1;
auto tk = new ListNode(-1),dumy = tk;
while(list1||list2){
if(!list1){
tk = tk->next = list2;
break;
}
else if(!list2){
tk = tk->next = list1;
break;
}
if(list1->val<list2->val){
tk = tk->next = new ListNode(list1->val);
list1 = list1->next;
}
else {
tk = tk->next = new ListNode(list2->val);
list2= list2->next;
}
}
return dumy->next;
}
};