-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path2.add-two-numbers.java
More file actions
31 lines (31 loc) · 836 Bytes
/
Copy path2.add-two-numbers.java
File metadata and controls
31 lines (31 loc) · 836 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
29
30
31
class Solution {
public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
int carry = 0;
ListNode ln = null, curr = null;
while(l1 != null || l2 != null){
int v1 = 0, v2 = 0;
if(l1 != null){
v1 = l1.val;
l1 = l1.next;
}
if(l2 != null){
v2 = l2.val;
l2 = l2.next;
}
int sum = v1 + v2 + carry;
carry = sum / 10;
int v = sum % 10;
if(ln == null){
ln = new ListNode(v);
curr = ln;
}else{
curr.next = new ListNode(v);
curr = curr.next;
}
}
if(carry > 0){
curr.next = new ListNode(carry);
}
return ln;
}
}