-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path21.合并两个有序链表.cpp
54 lines (46 loc) · 1.14 KB
/
21.合并两个有序链表.cpp
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
44
45
46
47
48
49
50
51
52
53
/*
* @lc app=leetcode.cn id=21 lang=cpp
*
* [21] 合并两个有序链表
*/
// @lc code=start
/**
* Definition for singly-linked list. */
#include<iostream>
#include<algorithm>
using namespace std;
// struct ListNode {
// int val;
// ListNode *next;
// ListNode(int x) : val(x), next(NULL) {}
// };
class Solution {
public:
ListNode* mergeTwoLists(ListNode* l1, ListNode* l2) {
if(l1==NULL&&l2!=NULL)return l2;
if(l1!=NULL&&l2==NULL)return l1;
if(l1==NULL&&l2==NULL)return NULL;
ListNode *p=l1, *q=l2;
ListNode *head = new ListNode(min(l1->val,l2->val));
ListNode *h=head;
while(true){
if(p==NULL){
head->next = q;
break;
}else if(q==NULL){
head->next = p;
break;
}
if(p->val < q->val){
head->next = new ListNode(p->val);
p = p->next;
}else{
head->next = new ListNode(q->val);
q = q->next;
}
head = head->next;
}
return h->next;
}
};
// @lc code=end