-
Notifications
You must be signed in to change notification settings - Fork 0
/
AddTwoNumbers.java
43 lines (43 loc) · 1.04 KB
/
AddTwoNumbers.java
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
32
33
34
35
36
37
38
39
40
41
42
43
/**
* @author : Anmol Agrawal
*/
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
class Solution {
public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
ListNode ans = new ListNode(0);
ListNode answer = ans;
int carry = 0;
while(true){
int sum = l1.val + l2.val + carry;
ans.next = new ListNode(sum % 10);
carry = sum / 10;
ans = ans.next;
if(l1.next == null && l2.next == null){
break;
}
if(l1.next != null){
l1 = l1.next;
}
else{
l1.val = 0;
}
if(l2.next != null){
l2= l2.next;
}
else{
l2.val = 0;
}
}
if(carry != 0){
ans.next = new ListNode(carry);
}
return answer.next;
}
}