forked from liuyubobobo/Play-Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
56 lines (45 loc) · 1.13 KB
/
main.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
54
55
56
/// Source : https://leetcode.com/problems/merge-two-sorted-lists/description/
/// Author : liuyubobobo
/// Time : 2017-12-01
#include <iostream>
using namespace std;
/// Definition for singly-linked list.
struct ListNode {
int val;
ListNode *next;
ListNode(int x) : val(x), next(NULL) {}
};
/// Iterative
/// Time Complexity: O(len(l1) + len(l2))
/// Space Complexity: O(1)
class Solution {
public:
ListNode* mergeTwoLists(ListNode* l1, ListNode* l2) {
ListNode* dummyHead = new ListNode(-1);
ListNode* p = dummyHead;
ListNode* l1p = l1;
ListNode* l2p = l2;
while(l1p != NULL && l2p != NULL){
if(l1p->val < l2p->val){
p->next = l1p;
l1p = l1p->next;
}
else{
p->next = l2p;
l2p = l2p->next;
}
p = p->next;
}
if(l1p != NULL)
p->next = l1p;
else
p->next = l2p;
ListNode* ret = dummyHead->next;
dummyHead->next = NULL;
delete dummyHead;
return ret;
}
};
int main() {
return 0;
}