-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathAddTwoNumbers.java
More file actions
51 lines (38 loc) · 1.79 KB
/
Copy pathAddTwoNumbers.java
File metadata and controls
51 lines (38 loc) · 1.79 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
// 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.
// See: https://leetcode.com/problems/add-two-numbers/
package leetcode.linkedlist;
import static leetcode.util.linkedlist.LinkedListUtil.*;
import leetcode.util.linkedlist.ListNode;
public class AddTwoNumbers {
public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
ListNode head = new ListNode(0);
ListNode result = head;
int reminder = 0;
while (l1 != null || l2 != null) {
int sum = (l1 != null ? l1.val : 0) + (l2 != null ? l2.val : 0) + reminder;
head.next = new ListNode(sum % 10);
reminder = sum / 10;
l1 = l1 != null ? l1.next : null;
l2 = l2 != null ? l2.next : null;
head = head.next;
}
if (reminder > 0) {
head.next = new ListNode(reminder);
}
return result.next;
}
public static void main(String[] args) {
AddTwoNumbers sln = new AddTwoNumbers();
ListNode res1 = sln.addTwoNumbers(initList(2, 4, 3), initList(5, 6, 4));
printLinkedList(res1); // 342 + 465 = 807.
ListNode res2 = sln.addTwoNumbers(initList(2, 4, 3), initList(5, 6, 4, 1));
printLinkedList(res2); // 342 + 1465 = 1807.
ListNode res3 = sln.addTwoNumbers(initList(5), initList(5));
printLinkedList(res3); // 5 + 5 = 10.
ListNode res4 = sln.addTwoNumbers(initList(1), initList(9, 9));
printLinkedList(res4); // 1 + 99 = 100.
}
}