-
Notifications
You must be signed in to change notification settings - Fork 0
/
2_add_two_numbers.cpp
54 lines (53 loc) · 1.67 KB
/
2_add_two_numbers.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
52
53
54
/**
* Definition for singly-linked list.
* 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) {}
* };
*/
#include <__config>
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) {}
};
class Solution {
public:
ListNode *addTwoNumbers(ListNode *l1, ListNode *l2) {
// Create a dummy head node to simplify the code
ListNode *dummy_head = new ListNode(0);
// Create pointers to the input lists and the current node in the new list
ListNode *p = l1, *q = l2, *current = dummy_head;
// Initialize the carry to 0
int carry = 0;
// Iterate over both lists
while (p != nullptr || q != nullptr) {
int x = (p != nullptr) ? p->val : 0;
int y = (q != nullptr) ? q->val : 0;
// Add carry from previous iteration to the current sum
int sum = carry + x + y;
// Update carry for the next iteration
carry = sum / 10;
// Create a new node with the digit value of the sum
current->next = new ListNode(sum % 10);
// Move to the next node
current = current->next;
if (p != nullptr)
p = p->next;
if (q != nullptr)
q = q->next;
}
// Check if there is a carry left after the last iteration
if (carry > 0) {
// Create a new node with the carry value
current->next = new ListNode(carry);
}
// Return the head of the new list (skip the dummy head node)
return dummy_head->next;
}
};