-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path165.h
44 lines (40 loc) · 1007 Bytes
/
165.h
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
/**
* Definition of ListNode
* class ListNode {
* public:
* int val;
* ListNode *next;
* ListNode(int val) {
* this->val = val;
* this->next = NULL;
* }
* }
*/
class Solution {
public:
/*
* @param l1: ListNode l1 is the head of the linked list
* @param l2: ListNode l2 is the head of the linked list
* @return: ListNode head of linked list
*/
ListNode * mergeTwoLists(ListNode * l1, ListNode * l2) {
// write your code here
ListNode dummyhead(0);
ListNode *cur = &dummyhead;
while(l1 != nullptr && l2 != nullptr){
if(l1->val < l2->val){
cur->next = l1;
cur = l1;
l1 = l1->next;
}
else{
cur->next = l2;
cur = l2;
l2 = l2->next;
}
}
if(l1) cur->next = l1;
else cur->next = l2;
return dummyhead.next;
}
};