File tree 2 files changed +18
-1
lines changed
main/java/com/fishercoder/solutions
test/java/com/fishercoder
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) {
13
13
return Integer .parseInt (sb .toString (), 2 );
14
14
}
15
15
}
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
+ }
16
27
}
Original file line number Diff line number Diff line change 12
12
13
13
public class _1290Test {
14
14
private static _1290 .Solution1 solution1 ;
15
+ private static _1290 .Solution2 solution2 ;
15
16
private static ListNode head ;
16
17
17
18
@ BeforeClass
18
19
public static void setup () {
19
20
solution1 = new _1290 .Solution1 ();
21
+ solution2 = new _1290 .Solution2 ();
20
22
}
21
23
22
24
@ Test
23
25
public void test1 () {
24
26
head = LinkedListUtils .createSinglyLinkedList (Arrays .asList (1 , 0 , 1 ));
25
27
assertEquals (5 , solution1 .getDecimalValue (head ));
26
28
}
27
-
29
+ @ Test
30
+ public void test2 () {
31
+ head = ListNode .createSinglyLinkedList (Arrays .asList (1 , 1 , 1 ));
32
+ assertEquals (7 , solution2 .getDecimalValue (head ));
33
+ }
28
34
}
You can’t perform that action at this time.
0 commit comments