Back to all solutions
#1290 - Convert Binary Number in a Linked List to Integer
Problem Description
Given `head` which is a reference node to a singly-linked list.
The value of each node in the linked list is either 0 or 1.
The linked list holds the binary representation of a number.
Return the decimal value of the number in the linked list.
Solution
/**
* Definition for singly-linked list.
* function ListNode(val) {
* this.val = val;
* this.next = null;
* }
*/
/**
* @param {ListNode} head
* @return {number}
*/
var getDecimalValue = function(head) {
let binary = String(head.val);
while (head.next !== null) {
head = head.next;
binary += head.val;
}
return parseInt(binary, 2);
};