Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * 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) {
- ListNode* x = new ListNode(0);
- ListNode* curr = x;
- ListNode* h1 = l1;
- ListNode* h2 = l2;
- int c = 0;
- while(h1 || h2 || c )
- {
- if (h1)
- {
- c += h1->val;
- h1 = h1->next;
- }
- if (h2)
- {
- c += h2->val;
- h2 = h2->next;
- }
- curr -> next = new ListNode( c % 10 );
- curr = curr -> next;
- c /= 10;
- }
- x = x -> next;
- return x;
- }
- };
- /*
- 342 + 465 = 807.
- 3 4 2
- 4 6 5
- -------
- 7 0 8
- */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement