-
Notifications
You must be signed in to change notification settings - Fork 40
/
Copy pathmergeTwoSortedLists.cpp
53 lines (42 loc) · 1.05 KB
/
mergeTwoSortedLists.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
// Merge Two Sorted Lists
#include <iostream>
struct ListNode {
int val;
ListNode *next;
ListNode(int x) : val(x), next(NULL) {}
};
ListNode* mergeTwoLists(ListNode* l1, ListNode* l2) {
ListNode* dummy = new ListNode(0);
ListNode* current = dummy;
while (l1 && l2) {
if (l1->val < l2->val) {
current->next = l1;
l1 = l1->next;
} else {
current->next = l2;
l2 = l2->next;
}
current = current->next;
}
if (l1) {
current->next = l1;
} else {
current->next = l2;
}
return dummy->next;
}
// Example usage
int main() {
ListNode* l1 = new ListNode(1);
l1->next = new ListNode(3);
l1->next->next = new ListNode(5);
ListNode* l2 = new ListNode(2);
l2->next = new ListNode(4);
l2->next->next = new ListNode(6);
ListNode* mergedList = mergeTwoLists(l1, l2);
while (mergedList != NULL) {
std::cout << mergedList->val << " ";
mergedList = mergedList->next;
}
return 0;
}