-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathP_2.java
More file actions
26 lines (23 loc) · 740 Bytes
/
Copy pathP_2.java
File metadata and controls
26 lines (23 loc) · 740 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
package leetcode.medium;
import utils.DataStructures.ListNode;
public class P_2 {
public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
final ListNode res = new ListNode(-1);
ListNode iter = res;
int carry = 0;
while (l1 != null || l2 != null) {
int next = carry;
if (l1 != null) { next += l1.val; }
if (l2 != null) { next += l2.val; }
carry = next / 10;
iter.next = new ListNode(next % 10);
if (l1 != null) { l1 = l1.next; }
if (l2 != null) { l2 = l2.next; }
iter = iter.next;
}
if (carry > 0) {
iter.next = new ListNode(carry);
}
return res.next;
}
}