File tree Expand file tree Collapse file tree 2 files changed +18
-1
lines changed
main/java/com/fishercoder/solutions
test/java/com/fishercoder Expand file tree Collapse file tree 2 files changed +18
-1
lines changed Original file line number Diff line number Diff 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}
Original file line number Diff line number Diff line change 1212
1313public 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}
You can’t perform that action at this time.
0 commit comments