Skip to content

Commit 83326b5

Browse files
authored
fix: add solution and testcase (fishercoder1534#156)
1 parent 21ec308 commit 83326b5

File tree

2 files changed

+18
-1
lines changed

2 files changed

+18
-1
lines changed

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

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,15 @@ public int getDecimalValue(ListNode head) {
1313
return Integer.parseInt(sb.toString(), 2);
1414
}
1515
}
16+
public static class Solution2 {
17+
public int getDecimalValue(ListNode head) {
18+
int sum = 0;
19+
while (head != null) {
20+
sum *= 2;
21+
sum += head.val;
22+
head = head.next;
23+
}
24+
return sum;
25+
}
26+
}
1627
}

src/test/java/com/fishercoder/_1290Test.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,17 +12,23 @@
1212

1313
public class _1290Test {
1414
private static _1290.Solution1 solution1;
15+
private static _1290.Solution2 solution2;
1516
private static ListNode head;
1617

1718
@BeforeClass
1819
public static void setup() {
1920
solution1 = new _1290.Solution1();
21+
solution2 = new _1290.Solution2();
2022
}
2123

2224
@Test
2325
public void test1() {
2426
head = LinkedListUtils.createSinglyLinkedList(Arrays.asList(1, 0, 1));
2527
assertEquals(5, solution1.getDecimalValue(head));
2628
}
27-
29+
@Test
30+
public void test2() {
31+
head = ListNode.createSinglyLinkedList(Arrays.asList(1, 1, 1));
32+
assertEquals(7, solution2.getDecimalValue(head));
33+
}
2834
}

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