Ctrl K

Value of a Bit Chain

Easy
The singly linked list bits stores a binary number: each node holds one bit, 0 or 1. The first node is the most significant bit and the last one is the least significant. Return the value of this number as a regular integer.
Example 1
bits=list_node([1, 1, 0]) → 6

Binary 110 is 4 + 2 = 6.

Example 2
bits=list_node([1, 0, 0, 1, 1]) → 19

10011₂ = 16 + 2 + 1 = 19.

Example 3
bits=list_node([0, 1]) → 1

A leading zero changes nothing: 01₂ = 1.

Constraints
  • The list has between 1 and 31 nodes
  • Node.val is 0 or 1
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsAdding Digit Lists MediumAdd One to a Digit Chain MediumForward Digit List Sum MediumGCD Between Neighbours MediumDouble the Digit Chain Medium
loading Python…
Input
bits=list_node([1, 1, 0])
Expected
6

Run checks the first 3 tests in your browser. Submit checks all 62 tests on the server, hidden ones included.