forked from edorado93/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path_002_AddTwoNumbers.java
51 lines (50 loc) · 1.2 KB
/
_002_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
44
45
46
47
48
49
50
51
package linkedlist;
/**
*
* Problem:
*
* You are given two non-empty linked lists representing two non-negative
* integers. The digits are stored in reverse order and each of their nodes
* contain a single digit. Add the two numbers and return it as a linked list.
*
* You may assume the two numbers do not contain any leading zero, except the
* number 0 itself.
*
* Input: (2 -> 4 -> 3) + (5 -> 6 -> 4) Output: 7 -> 0 -> 8
*
*/
public class _002_AddTwoNumbers {
public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
int carry = 0;
ListNode res = null;
ListNode result = null;
ListNode newNode = null;
while (l1 != null || l2 != null) {
int num1 = 0;
int num2 = 0;
if (l1 != null)
num1 = l1.val;
if (l2 != null)
num2 = l2.val;
newNode = new ListNode((num1 + num2 + carry) % 10);
if (res == null) {
res = newNode;
result = res;
} else {
res.next = newNode;
res = res.next;
}
carry = (num1 + num2 + carry) / 10;
if (l1 != null)
l1 = l1.next;
if (l2 != null)
l2 = l2.next;
}
// check if we still need to add one more node
if (carry != 0) {
ListNode one = new ListNode(carry);
res.next = one;
}
return result;
}
}