Convert Binary Number in a Linked List to Integer

Easy · Linked Lists · Linked List · Math · Bit Manipulation

Problem

Each node of a singly linked list holds one bit, 0 or 1. Read from head to tail, the list is a binary number with the most significant bit first.

Return the decimal value of that number.

Examples

Input: head = [1,1,0,1]
Output: 13

1101 in binary is 8 + 4 + 1 = 13.

Input: head = [0]
Output: 0

A single 0 bit is zero.

Input: head = [0,0,1,0]
Output: 2

Leading zeros add nothing: 0010 is 2.

Constraints

Approaches

Follow-up

The list holds at most 30 bits, so the value fits a 32-bit signed integer. For longer numbers you would return the value modulo a prime, applying the modulus after every step; the use cases do that for other bases, and read the bits least significant first.

Related problems

Start free on Cariara · Coding problems · System design studio

Starting Python
No results yet.

          
↑↓ move ↵ open esc close

Labs

Loading labs…

Run with your own input

Starting Python

Loading Python…