https://leetcode.cn/problems/add-two-numbers/description/
Finally I joined a foreign company's China branch to learn English and start a new journey.
PS: From now for me seems More leisurely, elegant, high-tech, and a little wise(in leadership) compared with (most) locals, not so management/leader-oriented but you must be mature enough to improve yourself. More freedom time is good and also bad. Therefore salary is relatively average(not elite, no definition because a little complex). But for me, it's enough.
Add Two Numbers
#include<iostream>
using namespace std;
struct ListNode
{
int val;
ListNode* next;
ListNode() : val(0), next(nullptr){};
ListNode(int x) : val(x), next(nullptr){};
ListNode(int x, ListNode *next) : val(x), next(next){};
};
ListNode* list(vector<int> v)
{
ListNode* dummy = new ListNode(), *p = dummy;
for (auto i : v)
{
p->next = new ListNode(i);
p = p->next;
}
return dummy->next;
}
ListNode* addTwoNumbers(ListNode* l1, ListNode* l2)
{
ListNode* dummy = new ListNode(), *p = dummy;
int carry = 0;
while(l1 || l2 || carry)
{
if (l1)
{
carry += l1->val;
l1 = l1->next;
}
if (l2)
{
carry += l2->val;
l2 = l2->next;
}
p->next = new ListNode(carry%10);
carry /= 10;
p = p->next;
}
return dummy->next;
}
int main()
{
ListNode* l1 = list({9,9,9,9,9,9,9});
ListNode* l2 = list({9,9,9,9});
ListNode* res = addTwoNumbers(l1, l2);
while(res)
{
cout << res->val << '\n';
res = res->next;
}
}
g++ leetcode2.cpp -std=c++11
-std=c++11 for list initialization {}.
标签:dummy,ListNode,val,next,Add,l2,Numbers,l1,LeetCode From: https://www.cnblogs.com/qianxinn/p/17792698.html