We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 57a55c3 commit 0a5dd4aCopy full SHA for 0a5dd4a
merge-two-sorted-lists/dylan-jung.cpp
@@ -0,0 +1,32 @@
1
+/**
2
+ * Definition for singly-linked list.
3
+ * struct ListNode {
4
+ * int val;
5
+ * ListNode *next;
6
+ * ListNode() : val(0), next(nullptr) {}
7
+ * ListNode(int x) : val(x), next(nullptr) {}
8
+ * ListNode(int x, ListNode *next) : val(x), next(next) {}
9
+ * };
10
+ */
11
+class Solution {
12
+public:
13
+ ListNode* mergeTwoLists(ListNode* list1, ListNode* list2) {
14
+ ListNode dummy;
15
+ ListNode* tail = &dummy;
16
+
17
+ while (list1 && list2) {
18
+ if (list1->val < list2->val) {
19
+ tail->next = list1;
20
+ list1 = list1->next;
21
+ } else {
22
+ tail->next = list2;
23
+ list2 = list2->next;
24
+ }
25
+ tail = tail->next;
26
27
28
+ tail->next = list1 ? list1 : list2;
29
30
+ return dummy.next;
31
32
+};
0 commit comments