Skip to content

Commit 4f21ae0

Browse files
refactor 445
1 parent 3dc28bd commit 4f21ae0

File tree

2 files changed

+31
-28
lines changed

2 files changed

+31
-28
lines changed

src/main/java/com/fishercoder/solutions/_445.java

Lines changed: 28 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
/**
1010
* 445. Add Two Numbers II
11+
*
1112
* You are given two non-empty linked lists representing two non-negative integers.
1213
* The most significant digit comes first and each of their nodes contain a single digit. Add the two numbers and return it as a linked list.
1314
@@ -23,36 +24,38 @@
2324
*/
2425
public class _445 {
2526

26-
public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
27-
Deque<Integer> stack1 = popIntoStack(l1);
28-
Deque<Integer> stack2 = popIntoStack(l2);
27+
public static class Solution1 {
28+
public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
29+
Deque<Integer> stack1 = popIntoStack(l1);
30+
Deque<Integer> stack2 = popIntoStack(l2);
2931

30-
int sum = 0;
31-
ListNode list = new ListNode(0);
32-
while (!stack1.isEmpty() || !stack2.isEmpty()) {
33-
if (!stack1.isEmpty()) {
34-
sum += stack1.removeFirst();
35-
}
36-
if (!stack2.isEmpty()) {
37-
sum += stack2.removeFirst();
32+
int sum = 0;
33+
ListNode list = new ListNode(0);
34+
while (!stack1.isEmpty() || !stack2.isEmpty()) {
35+
if (!stack1.isEmpty()) {
36+
sum += stack1.removeFirst();
37+
}
38+
if (!stack2.isEmpty()) {
39+
sum += stack2.removeFirst();
40+
}
41+
list.val = sum % 10;
42+
ListNode head = new ListNode(sum / 10);
43+
head.next = list;
44+
list = head;
45+
sum /= 10;
3846
}
39-
list.val = sum % 10;
40-
ListNode head = new ListNode(sum / 10);
41-
head.next = list;
42-
list = head;
43-
sum /= 10;
47+
return list.val == 0 ? list.next : list;
4448
}
45-
return list.val == 0 ? list.next : list;
46-
}
4749

48-
private Deque<Integer> popIntoStack(ListNode head) {
49-
ListNode tmp = head;
50-
Deque<Integer> stack = new ArrayDeque<>();
51-
while (tmp != null) {
52-
stack.push(tmp.val);
53-
tmp = tmp.next;
50+
private Deque<Integer> popIntoStack(ListNode head) {
51+
ListNode tmp = head;
52+
Deque<Integer> stack = new ArrayDeque<>();
53+
while (tmp != null) {
54+
stack.push(tmp.val);
55+
tmp = tmp.next;
56+
}
57+
return stack;
5458
}
55-
return stack;
5659
}
5760

5861

src/test/java/com/fishercoder/_445Test.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,12 @@
1212
* Created by fishercoder on 5/13/17.
1313
*/
1414
public class _445Test {
15-
private static _445 test;
15+
private static _445.Solution1 solution1;
1616
private static _445.Solution2 solution2;
1717

1818
@BeforeClass
1919
public static void setup() {
20-
test = new _445();
20+
solution1 = new _445.Solution1();
2121
solution2 = new _445.Solution2();
2222
}
2323

@@ -29,7 +29,7 @@ public void test1() {
2929

3030
ListNode expected = LinkedListUtils.contructLinkedList(new int[]{7, 8, 0, 7});
3131

32-
assertEquals(expected, test.addTwoNumbers(l1, l2));
32+
assertEquals(expected, solution1.addTwoNumbers(l1, l2));
3333
}
3434

3535
@Test

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy