forked from examplehub/Java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAddTwoNumbers.java
More file actions
79 lines (69 loc) · 1.8 KB
/
AddTwoNumbers.java
File metadata and controls
79 lines (69 loc) · 1.8 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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
package com.examplehub.leetcode.middle;
/** https://leetcode.com/problems/add-two-numbers/ */
public class AddTwoNumbers {
/* solution1 can't process large input */
public static ListNode solution1(ListNode l1, ListNode l2) {
StringBuilder builder1 = new StringBuilder();
while (l1 != null) {
builder1.append(l1.val);
l1 = l1.next;
}
StringBuilder builder2 = new StringBuilder();
while (l2 != null) {
builder2.append(l2.val);
l2 = l2.next;
}
/* calculate sum of two lists */
int sum =
Integer.parseInt(builder1.reverse().toString())
+ Integer.parseInt(builder2.reverse().toString());
/* construct a singly linked list with sum of two lists */
ListNode head = null;
ListNode tail = null;
do {
if (head == null) {
tail = head = new ListNode(sum % 10);
} else {
tail.next = new ListNode(sum % 10);
tail = tail.next;
}
sum /= 10;
} while (sum != 0);
return head;
}
public static ListNode solution2(ListNode l1, ListNode l2) {
ListNode dummyHead = new ListNode(0);
ListNode p = l1, q = l2, curr = dummyHead;
int carry = 0;
while (p != null || q != null) {
int x = p != null ? p.val : 0;
int y = q != null ? q.val : 0;
int sum = carry + x + y;
carry = sum / 10;
curr.next = new ListNode(sum % 10);
curr = curr.next;
if (p != null) {
p = p.next;
}
if (q != null) {
q = q.next;
}
}
if (carry > 0) {
curr.next = new ListNode(carry);
}
return dummyHead.next;
}
}
class ListNode {
int val;
ListNode next;
public ListNode() {}
public ListNode(int val) {
this.val = val;
}
ListNode(int val, ListNode next) {
this.val = val;
this.next = next;
}
}