Algorithm Puzzles ~~everyday~~ ~~every week~~ sometimes: Add two numbers

Puzzle Puzzle from leetcode :

You are given two non-empty linked lists representing two non-negative integers. The digits are stored in reverse order and each of their nodes contain a single digit. Add the two numbers and return it as a linked list.

You may assume the two numbers do not contain any leading zero, except the number 0 itself.

Example:

Input: (2 -> 4 -> 3) + (5 -> 6 -> 4) Output: 7 -> 0 -> 8 Explanation: 342 + 465 = 807.

The template code provided by leetcode:

1 2 3 4 5 6 7 8 9 10 11 12 13 14 class Solution {public : ListNode* addTwoNumbers (ListNode* l1, ListNode* l2) { } };

Solving My first came out solution just like how we do sum on draft paper:

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 class Solution {public : ListNode* addTwoNumbers (ListNode* l1, ListNode* l2) { ListNode* result = new ListNode(0 ); ListNode* resultHead = result; ListNode emptyNode{0 }; int carry = 0 ; while (1 ){ carry = addTwoNode(l1, l2, carry, result); if ((carry == 0 ) && (l1->next == nullptr ) && (l2->next == nullptr )){ return resultHead; } else { result->next = new ListNode(0 ); result = result->next; l1 = l1->next?l1->next:&emptyNode; l2 = l2->next?l2->next:&emptyNode; } } } int addTwoNode (ListNode* l1, ListNode* l2, int carry, ListNode* result) { result->val = (l1->val + l2->val + carry); if (result->val >= 10 ){ result->val -= 10 ; return 1 ; }else { return 0 ; } } };

Time complexity: O(n)

It’s do a little optimization:

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 class Solution {public : ListNode* addTwoNumbers (ListNode* l1, ListNode* l2) { ListNode* result = new ListNode(0 ); ListNode* resultHead = result; ListNode emptyNode{0 }; int carry = 0 ; while (1 ){ carry = addTwoNode(l1->val, l2->val, result); if ((carry == 0 ) && (l1->next == nullptr ) && (l2->next == nullptr )){ return resultHead; } else { result->next = new ListNode(carry); result = result->next; l1 = l1->next?l1->next:&emptyNode; l2 = l2->next?l2->next:&emptyNode; } } } int addTwoNode (const int & l1, const int & l2, ListNode* result) { result->val += (l1 + l2); if (result->val >= 10 ){ result->val -= 10 ; return 1 ; }else { return 0 ; } } };

Super! Better than 99.99%!