-
Notifications
You must be signed in to change notification settings - Fork 0
/
2.AddTwoNumbers.cpp
51 lines (42 loc) · 1.35 KB
/
2.AddTwoNumbers.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
///
/// LeetCode
/// By Reza Ebrahimi <reza.ebrahimi.dev@gmail.com>
///
/// Add Two Numbers Problem
/// https://leetcode.com/problems/add-two-numbers
///
/// Time Submitted | Status | Runtime | Memory | Language
/// 2020/03/11 | Accepted | 20 ms | 11.1 MB| cpp
///
/// Runtime: 20 ms, faster than 98.04% of C++ online submissions for Add Two Numbers.
/// Memory Usage: 11.1 MB, less than 5.14% of C++ online submissions for Add Two Numbers.
///
/*
Definition for singly-linked list.
struct ListNode {
int val;
ListNode *next;
ListNode(int x) : val(x), next(NULL) {}
};
*/
#include <iostream>
class Solution {
public:
ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) {
ListNode *main_node = new ListNode(0);
ListNode *node = main_node;
int carry = 0;
while (l1 != nullptr || l2 != nullptr) {
int val1 = l1 != nullptr ? l1->val : 0;
int val2 = l2 != nullptr ? l2->val : 0;
int sum = val1 + val2 + carry;
carry = sum / 10;
node->next = new ListNode(sum % 10);
node = node->next;
l1 = l1 != nullptr ? l1->next : nullptr;
l2 = l2 != nullptr ? l2->next : nullptr;
}
node->next = carry ? new ListNode(carry) : nullptr;
return main_node->next;
}
};