Skip to content

Commit 24379ec

Browse files
committed
feat: fix for CI warnings in 2.cpp
1 parent 76c791b commit 24379ec

File tree

1 file changed

+21
-19
lines changed

1 file changed

+21
-19
lines changed

leetcode/src/2.cpp

+21-19
Original file line numberDiff line numberDiff line change
@@ -3,36 +3,38 @@
33
* Add the two numbersand return the sum as a linked list.
44
*/
55

6-
/*
6+
#include <memory>
7+
8+
/*
79
* Definition for singly-linked list.
810
*/
911
struct ListNode {
1012
int val;
1113
ListNode *next;
12-
ListNode() : val(0), next(nullptr) {}
14+
explicit ListNode() : val(0), next(nullptr) {}
1315
ListNode(int x) : val(x), next(nullptr) {}
1416
ListNode(int x, ListNode *next) : val(x), next(next) {}
1517
};
1618

1719
class Solution {
1820
public:
19-
ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) {
20-
int carry = 0;
21-
ListNode head;
22-
ListNode* sum = &head;
23-
24-
while (l1 || l2) {
25-
int val = carry + (l1 ? l1->val : 0) + (l2 ? l2->val : 0);
26-
carry = val / 10;
27-
sum->next = new ListNode(val % 10);
28-
sum = sum->next;
29-
l1 = (l1 ? l1->next : nullptr);
30-
l2 = (l2 ? l2->next : nullptr);
31-
}
32-
if (carry) {
33-
sum->next = new ListNode(carry);
34-
}
21+
ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) {
22+
int carry = 0;
23+
std::unique_ptr<ListNode> head(new ListNode(0));
24+
ListNode* sum = head.get();
3525

36-
return head.next;
26+
while(l1 || l2) {
27+
int val = carry + (l1 ? l1->val : 0) + (l2 ? l2->val : 0);
28+
carry = val/10;
29+
sum->next = new ListNode(val%10);
30+
sum = sum->next;
31+
l1 = (l1 ? l1->next : nullptr);
32+
l2 = (l2 ? l2->next : nullptr);
3733
}
34+
if(carry){
35+
sum->next = new ListNode(carry);
36+
}
37+
38+
return head->next;
39+
}
3840
};

0 commit comments

Comments
 (0)