Show problems as
Classic problem #1290EasyLinked ListMath

Convert Binary Number in a Linked List to Integer

Real-world version: Reading a Punch Chain as a Number
Tracked in this browser only
Read Convert Binary Number in a Linked List to Integer on LeetCode(opens in a new tab)

The statement here is our own: the same inputs, outputs and technique.

A tally machine punches cards and clips them one behind another. Every card carries a single hole state, either 0 or 1, plus a clip fastened to the card behind it, and the card at the end has an empty clip. Nothing in the machine records how many cards a chain holds.

Because the harness passes plain JSON, the chain reaches you as punches, listing the hole states in clip order starting at the front card. There is always at least one card.

Read the chain as a number written in base two, with the front card carrying the largest place value and the card at the end carrying the ones. Return that number.

Examples

Example 1

Input
punches = [1, 1, 0]
Output
6

The front card is worth four, the middle one two, and the card at the end nothing, so the chain reads six.

Example 2

Input
punches = [1, 0, 0, 1]
Output
9

Only the front card and the card at the end are punched, worth eight and one, which adds to nine.

Example 3

Input
punches = [0, 0, 1]
Output
1

The leading unpunched cards carry no weight, so only the ones place counts.

Constraints

  • 1 <= punches.length <= 30
  • 0 <= punches[i] <= 1

The signature

The editor starts with this. It matches the parent problem's shape, so a solution that works here works on a standard judge unchanged.

Python
def get_decimal_value(punches: list[int]) -> int:
Java
public int getDecimalValue(int[] punches)