From dc1f0c4c0096013799416664894c5194dc7e1f52 Mon Sep 17 00:00:00 2001 From: Yuqian Yang Date: Fri, 28 Feb 2025 23:13:39 +0800 Subject: chore(store): move everything to store. --- store/works/solutions/leetcode/cpp/2.cpp | 75 ++++++++++++++++++++++++++++++++ 1 file changed, 75 insertions(+) create mode 100644 store/works/solutions/leetcode/cpp/2.cpp (limited to 'store/works/solutions/leetcode/cpp/2.cpp') diff --git a/store/works/solutions/leetcode/cpp/2.cpp b/store/works/solutions/leetcode/cpp/2.cpp new file mode 100644 index 0000000..cb954ae --- /dev/null +++ b/store/works/solutions/leetcode/cpp/2.cpp @@ -0,0 +1,75 @@ +struct ListNode +{ + int val; + ListNode *next; + ListNode(int x) : val(x), next(NULL) {} +}; + +class Solution +{ +public: + ListNode *addTwoNumbers(ListNode *l1, ListNode *l2) + { + ListNode *result; + ListNode *tail; + int carry = 0; + + { + int sum = l1->val + l2->val; + if (sum > 9) + { + carry = 1; + sum -= 10; + } + + result = new ListNode(sum); + tail = result; + + l1 = l1->next; + l2 = l2->next; + } + + while (l1 || l2) + { + int sum = (l1 ? l1->val : 0) + (l2 ? l2->val : 0) + carry; + if (sum > 9) + { + carry = 1; + sum -= 10; + } + else + { + carry = 0; + } + tail->next = new ListNode(sum); + tail = tail->next; + + if (l1) + l1 = l1->next; + if (l2) + l2 = l2->next; + } + + if (carry) + { + tail->next = new ListNode(1); + } + + return result; + } +}; + +int main() +{ + ListNode *l1 = new ListNode(2); + l1->next = new ListNode(4); + l1->next = new ListNode(3); + + ListNode *l2 = new ListNode(5); + l2->next = new ListNode(6); + l2->next = new ListNode(4); + + ListNode *result = Solution{}.addTwoNumbers(l1, l2); + + return 0; +} -- cgit v1.2.3