commit b46ca7957ef56b5e80afe398c35ea6c6a0735a44
parent 43adbe31a527c08c4168a471226327d1e873b24d
Author: Dimitrije Dobrota <mail@dimitrijedobrota.com>
Date: Mon, 23 Jan 2023 16:30:33 +0100
LeetCode 75 I: Day 3
Diffstat:
2 files changed, 4 insertions(+), 15 deletions(-)
diff --git a/Problems/0021.cpp b/Problems/0021.cpp
@@ -1,8 +1,7 @@
class Solution {
public:
ListNode *mergeTwoLists(ListNode *list1, ListNode *list2) {
- ListNode *head, *t;
- t = head = new ListNode();
+ ListNode head, *t = &head;
while (list1 && list2) {
if (list1->val < list2->val) {
@@ -14,16 +13,7 @@ public:
}
}
- while (list1) {
- t = t->next = list1;
- list1 = list1->next;
- }
-
- while (list2) {
- t = t->next = list2;
- list2 = list2->next;
- }
-
- return head->next;
+ t->next = list1 ? list1 : list2;
+ return head.next;
}
};
diff --git a/Problems/0206.cpp b/Problems/0206.cpp
@@ -3,8 +3,7 @@ public:
ListNode *reverseList(ListNode *head) {
ListNode *p, *q, *r;
- p = head;
- q = nullptr;
+ p = head, q = nullptr;
while (p) {
r = q;
q = p;