forked from kamyu104/LintCode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
merge-two-sorted-lists.cpp
48 lines (44 loc) · 1.16 KB
/
merge-two-sorted-lists.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
// Time: O(n)
// Space: O(1)
/**
* Definition of ListNode
* class ListNode {
* public:
* int val;
* ListNode *next;
* ListNode(int val) {
* this->val = val;
* this->next = NULL;
* }
* }
*/
class Solution {
public:
/**
* @param ListNode l1 is the head of the linked list
* @param ListNode l2 is the head of the linked list
* @return: ListNode head of linked list
*/
ListNode *mergeTwoLists(ListNode *l1, ListNode *l2) {
// Creates a dummy head.
ListNode * dummy_head = new ListNode(INT_MIN);
auto tail = dummy_head;
while (l1 && l2) {
AppendNode(l1->val < l2->val ? &l1 : &l2, &tail);
}
if (l1) {
// Appends the remaining nodes of l1.
AppendNode(&l1, &tail);
} else if (l2) {
// Appends the remaining nodes of l2.
AppendNode(&l2, &tail);
}
return dummy_head->next;
}
void AppendNode(ListNode ** node,
ListNode ** tail) {
(*tail)->next = *node;
*tail = *node; // Resets tail to the last node.
*node = (*node)->next;
}
};