Convert Binary Number in a Linked List to Integer

Given head, a reference to the head node of a singly linked list in which every node holds 0 or 1, the list is the binary representation of a number with the most significant bit at the head. Return the decimal value of that number.

30:00

Convert Binary Number in a Linked List to Integer
easy
Free written solution
Topics
Companies

Given head, a reference to the head node of a singly linked list in which every node holds 0 or 1, the list is the binary representation of a number with the most significant bit at the head. Return the decimal value of that number.

Example 1:
Input: {"head":[1,0,1]}
Output: 5
Constraints:
  • The linked list is not empty.

  • The number of nodes will not exceed 3030.

  • Each node's value is either 00 or 11.

Test Cases
Input (Stdin)
Expected Output (Stdout)
Execution Result

Run code to see results.

Input
arr ={"head":[1,0,1]}

Start with num = 0 and point node at the head. The head holds the most significant bit.

list, with each bit's current weight in num

node

1

·

node

0

·

node

1

·

null

num = num × 2 + node.val

waiting for the next bit

num

0

binary 0

Variables
VariableValue
num0
DepthFunction Call
Stack empty
0/7