Created
February 22, 2019 06:46
-
-
Save NeraSnow/056d8ea504fd1507d49858e260a5035b to your computer and use it in GitHub Desktop.
LeetCode 2. Add Two Numbers draft
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Definition for singly-linked list. | |
* struct ListNode { | |
* int val; | |
* ListNode *next; | |
* ListNode(int x) : val(x), next(NULL) {} | |
* }; | |
*/ | |
class Solution { | |
public: | |
ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) { | |
ListNote* head = new ListNode; | |
while(l1->next != NULL && l2->next != NULL) | |
{ | |
if (l1->next == NULL) | |
l1Val = l1->val; | |
l2Val = l2->val; | |
bool carryOver = false; | |
int sum = l1Val + l2Val; | |
if (sum > 10) | |
{ | |
sum %= 10; | |
carryOver = true; | |
} | |
ListNode* nextNode = new ListNode(sum); | |
} | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment