Skip to content

Commit 6f32bb8

Browse files
committed
Add solution #1290
1 parent 0840d5c commit 6f32bb8

File tree

1 file changed

+32
-0
lines changed

1 file changed

+32
-0
lines changed
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/**
2+
* 1290. Convert Binary Number in a Linked List to Integer
3+
* https://leetcode.com/problems/convert-binary-number-in-a-linked-list-to-integer/
4+
* Difficulty: Easy
5+
*
6+
* Given `head` which is a reference node to a singly-linked list.
7+
* The value of each node in the linked list is either 0 or 1.
8+
* The linked list holds the binary representation of a number.
9+
* Return the decimal value of the number in the linked list.
10+
*/
11+
12+
/**
13+
* Definition for singly-linked list.
14+
* function ListNode(val) {
15+
* this.val = val;
16+
* this.next = null;
17+
* }
18+
*/
19+
/**
20+
* @param {ListNode} head
21+
* @return {number}
22+
*/
23+
var getDecimalValue = function(head) {
24+
let binary = String(head.val);
25+
26+
while (head.next !== null) {
27+
head = head.next;
28+
binary += head.val;
29+
}
30+
31+
return parseInt(binary, 2);
32+
};

0 commit comments

Comments
 (0)