Skip to content

Commit d9860a5

Browse files
committed
1290 Convert Binary Number in a LinkedList to Integer
1 parent 2eb2ab7 commit d9860a5

File tree

1 file changed

+32
-0
lines changed

1 file changed

+32
-0
lines changed

Diff for: getDecimalValue.ts

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
console.log("ffff");
2+
3+
/**
4+
* Definition for singly-linked list.
5+
**/
6+
class ListNode {
7+
val: number;
8+
next: ListNode | null;
9+
10+
constructor(val: number = 0, next: ListNode | null = null) {
11+
this.val = val;
12+
this.next = next;
13+
}
14+
}
15+
16+
function getDecimalValue(head: ListNode | null): number {
17+
if (head === null) return 0
18+
let result=0;
19+
20+
while (head) {
21+
result = head.val + result * 2;
22+
head = head.next
23+
}
24+
return result
25+
};
26+
27+
const head = new ListNode(1);
28+
head.next = new ListNode(0);
29+
head.next.next = new ListNode(1);
30+
31+
const testing = getDecimalValue(head);
32+
console.log(testing);

0 commit comments

Comments
 (0)