-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAddTwoNubs.java
More file actions
53 lines (42 loc) · 1.13 KB
/
AddTwoNubs.java
File metadata and controls
53 lines (42 loc) · 1.13 KB
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
44
45
46
47
48
49
50
51
52
53
package Leetcode;
class ListNode{
public ListNode(int val) {
this.val = val;
}
int val;
ListNode next;
}
/**
*
* Input: (2 -> 4 -> 3) + (5 -> 6 -> 4)
* Output: 7 -> 0 -> 8
* Explanation: 342 + 465 = 807.
*
*/
public class AddTwoNubs {
public ListNode addTwoNumbers(ListNode n1, ListNode n2){
ListNode res = new ListNode(0);
ListNode tmpHead = res;
int sum = 0;
while (n1!=null || n2!=null){
sum = sum / 10; // sum=15 , 15/10 => sum = 1 , consider the carry forward result
if (n1!=null){
sum = sum + n1.val;
n1 = n1.next;
}
if (n2!=null){
sum = sum + n2.val;
n2 = n2.next;
}
ListNode tmp = new ListNode(sum % 10); // 15 % 10 => 5
tmpHead.next = tmp;
tmpHead = tmpHead.next;
}
return res.next;
}
public static void main(String[] args) {
int sum = 12;
System.out.println(sum%10);
System.out.println(sum/10);
}
}