This is one of those problems where the clean idea matters more than the amount of code. For 2. Add Two Numbers, the solution in this repository is mainly a straightforward implementation 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: straightforward implementation.
The notes already sitting in the source point us in the right direction:
- time: O(max(m,n)), space: O(max(m,n))
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, reverse.
Guide
Why?
The point of the implementation is not to make the code longer. It is to avoid doing the same thinking twice.
- 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(max(m,n)), space: O(max(m,n))
- Space: O(n) in the usual case for auxiliary containers or recursion
Guide
C++ Solution
Your submission
The accepted solution
01//Runtime: 32 ms, faster than 79.81% of C++ online submissions for Add Two Numbers.
02//Memory Usage: 71.1 MB, less than 26.28% of C++ online submissions for Add Two Numbers.
03//time: O(max(m,n)), space: O(max(m,n))
04/**
05 * Definition for singly-linked list.
06 * struct ListNode {
07 * int val;
08 * ListNode *next;
09 * ListNode() : val(0), next(nullptr) {}
10 * ListNode(int x) : val(x), next(nullptr) {}
11 * ListNode(int x, ListNode *next) : val(x), next(next) {}
12 * };
13 */
14class Solution {
15public:
16 ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) {
17 ListNode *prehead = new ListNode();
18 ListNode *pre = prehead;
19 ListNode *cur1 = l1, *cur2 = l2;
20 int digit = 0;
21
22 while(cur1 || cur2 || digit > 0){
23 if(cur1){
24 digit += cur1->val;
25 cur1 = cur1->next;
26 }
27 if(cur2){
28 digit += cur2->val;
29 cur2 = cur2->next;
30 }
31 ListNode *tmp = new ListNode(digit%10);
32 pre->next = tmp;
33 pre = pre->next;
34 digit/=10;
35 }
36
37 return prehead->next;
38 }
39};
40
41//Follow up
42//What if the the digits in the linked list are stored in non-reversed order? For example:
43//(3 -> 4 -> 2) + (4 -> 6 -> 5) = 8 -> 0 -> 7
44//reverse l1 and l2 first, do the algo, and then reverse the answer
45//reverse function:
46ListNode* reverse(ListNode* h){
47 if(!h || !h->next) return h;
48
49 ListNode* prev = nullptr;
50 ListNode* cur = h;
51 while(cur){
52 ListNode* next = cur->next;
53 cur->next = prev;
54 prev = cur;
55 cur = next;
56 }
57
58 return prev;
59}
Cost