This is one of those problems where the clean idea matters more than the amount of code. For 445. Add Two Numbers II, the solution in this repository is mainly a stack solution.
Guide
What?
The first job is to translate the English prompt into state, transition, and stopping conditions. Instead of trying to be clever immediately, read the code as a sequence of questions:
- What state are we keeping?
- How do we move from one state to the next?
- When do we know the answer is already determined?
For this file, the main tools are: stack.
Guide
When?
This is the kind of solution you want when the problem has structure hiding inside a messy-looking input. The accepted code reduces that pressure by storing exactly the information that remains useful later.
The important function names to track are addTwoNumbers.
Guide
Why?
The point of the implementation is not to make the code longer. It is to avoid doing the same thinking twice.
- The stack stores unfinished context, which is usually the cleanest way to handle nested or monotonic structure.
- The final return is not magic; it is the invariant after the loops or recursion have finished doing their accounting.
Guide
How?
Walk through the solution in this order:
- Start from the smallest reliable state.
- Expand one legal move at a time.
- Cache, count, or merge information as soon as it becomes settled.
- Let the final stored value answer the original question.
The most important competitive-programming habit here is to trust the invariant. Once the invariant is right, the loops become much less scary.
Guide
Complexity
- Time: O(n) to O(n log n), depending on the dominant loop or data structure operation
- Space: O(n) in the usual case for auxiliary containers or recursion
Guide
C++ Solution
Your submission
The accepted solution
01//Runtime: 24 ms, faster than 93.79% of C++ online submissions for Add Two Numbers II.
02//Memory Usage: 11.6 MB, less than 51.85% of C++ online submissions for Add Two Numbers II.
03
04/**
05 * Definition for singly-linked list.
06 * struct ListNode {
07 * int val;
08 * ListNode *next;
09 * ListNode(int x) : val(x), next(NULL) {}
10 * };
11 */
12class Solution {
13public:
14 ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) {
15 vector<int> v1, v2;
16 ListNode* cur = l1;
17 while(cur){
18 v1.insert(v1.begin(), cur->val);
19 cur = cur->next;
20 }
21
22 cur = l2;
23 while(cur){
24 v2.insert(v2.begin(), cur->val);
25 cur = cur->next;
26 }
27
28 //reverse room for carry-in
29 int newSize = max(v1.size(), v2.size()) + 1;
30 v1.resize(newSize);
31 v2.resize(newSize);
32 for(int i = 0; i < newSize-1; i++){
33 v1[i] += v2[i];
34 if(v1[i] >= 10){
35 v1[i]-=10;
36 v1[i+1]++;
37 }
38 // cout << v1[i] << " ";
39 }
40 // cout << endl;
41
42 ListNode* head = new ListNode();
43 cur = head;
44 //skip leading 0s
45 int idx = newSize-1;
46 // cout << "idx: " << idx << endl;
47 while(idx >= 0 && v1[idx] == 0){
48 idx--;
49 }
50 // cout << "idx: " << idx << endl;
51 for(; idx >= 0; idx--){
52 cur->val = v1[idx];
53 if(idx > 0){
54 //not the last node
55 cur->next = new ListNode();
56 cur = cur->next;
57 }
58 }
59
60 return head;
61 }
62};
63
64//Stack
65//https://leetcode.com/problems/add-two-numbers-ii/discuss/92623/Easy-O(n)-Java-Solution-using-Stack
66//Runtime: 32 ms, faster than 54.73% of C++ online submissions for Add Two Numbers II.
67//Memory Usage: 13.7 MB, less than 22.22% of C++ online submissions for Add Two Numbers II.
68//time: O(n)
69
70/**
71 * Definition for singly-linked list.
72 * struct ListNode {
73 * int val;
74 * ListNode *next;
75 * ListNode(int x) : val(x), next(NULL) {}
76 * };
77 */
78class Solution {
79public:
80 ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) {
81 stack<int> stk1, stk2;
82 ListNode* cur = l1;
83 while(cur){
84 stk1.push(cur->val);
85 cur = cur->next;
86 }
87
88 cur = l2;
89 while(cur){
90 stk2.push(cur->val);
91 cur = cur->next;
92 }
93
94 ListNode* head = new ListNode(0);
95 int d = 0;
96
97 while(!stk1.empty() || !stk2.empty()){
98 if(!stk1.empty()){
99 d += stk1.top();
100 stk1.pop();
101 }
102 if(!stk2.empty()){
103 d += stk2.top();
104 stk2.pop();
105 }
106 head->val = d%10;
107 ListNode* prehead = new ListNode(d/10);
108 prehead->next = head;
109 head = prehead;
110 d = d/10;
111 }
112
113 return head->val == 0 ? head->next : head;
114 }
115};
Cost