Skip to content

Instantly share code, notes, and snippets.

@eternalharvest
Last active May 3, 2019 01:33
Show Gist options
  • Save eternalharvest/c7574126319d64760339da83cee3fc6d to your computer and use it in GitHub Desktop.
Save eternalharvest/c7574126319d64760339da83cee3fc6d to your computer and use it in GitHub Desktop.
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
private:
static constexpr char _lut_mod[20] = {
0, 1, 2, 3, 4, 5, 6, 7, 8, 9,
0, 1, 2, 3, 4, 5, 6, 7, 8, 9
};
public:
ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) {
struct ListNode head(0), *curr;
int ax, c = 0;
for (curr = &head; l1 != NULL || l2 != NULL; curr = curr->next) {
ax = (l1 == NULL ? 0 : l1->val) + (l2 == NULL ? 0 : l2->val) + c;
c = ax >= 10;
curr->next = new struct ListNode(_lut_mod[ax]);
l1 = l1 == NULL ? NULL : l1->next;
l2 = l2 == NULL ? NULL : l2->next;
}
if (c != 0) {
curr->next = new struct ListNode(c);
}
return head.next;
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment