-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path21.cpp
More file actions
41 lines (39 loc) · 1.18 KB
/
21.cpp
File metadata and controls
41 lines (39 loc) · 1.18 KB
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
// Author: btjanaka (Bryon Tjanaka)
// Problem: (Leetcode) 21
// Title: Merge Two Sorted Lists
// Link: https://leetcode.com/problems/merge-two-sorted-lists
// Idea: See code comments. We just keep selecting the next largest element.
// Difficulty: easy
// Tags: linked-list, sorting
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
ListNode* mergeTwoLists(ListNode* l1, ListNode* l2) {
ListNode* head = new ListNode(0);
ListNode* tail = head;
while (l1 != NULL || l2 != NULL) {
// Use l1 if l2 is NULL (then we would know that l1 has to be non-null,
// since the while loop would exit otherwise), or if l1 is != NULL and
// l1 has a value less than l2 (we know l2 can be safely called because
// it passed the first check and failed to short circuit).
//
// Otherwise, use l2.
if (l2 == NULL || (l1 != NULL && l1->val < l2->val)) {
tail->next = l1;
l1 = l1->next;
} else {
tail->next = l2;
l2 = l2->next;
}
tail = tail->next;
}
return head->next;
}
};