-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSolution2.cpp
More file actions
28 lines (26 loc) · 755 Bytes
/
Solution2.cpp
File metadata and controls
28 lines (26 loc) · 755 Bytes
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
//
// Solution2.cpp
// Algorithm
//
// Created by Pancf on 2019/12/29.
// Copyright © 2019 Pancf. All rights reserved.
//
#include "Solution2.hpp"
Solution2::ListNode * Solution2::addTwoNumbers(ListNode *l1, ListNode *l2) {
auto l1_cur = l1;
auto l2_cur = l2;
ListNode *rv = new ListNode{-1};
ListNode *prev = rv;
int carry = 0;
while (l1_cur || l2_cur || carry) {
auto val = (l1_cur ? l1_cur->val : 0) + (l2_cur ? l2_cur->val : 0) + carry;
carry = val / 10;
val = val % 10;
ListNode *node = new ListNode{val};
prev->next = node;
prev = prev->next;
l1_cur = l1_cur ? l1_cur->next : l1_cur;
l2_cur = l2_cur ? l2_cur->next : l2_cur;
}
return rv->next;
}