Binary Number in a Linked List to Integer

EasyLinked Listbit manipulation

Solve this the way you would in the interview: out loud, on a timer, with an AI interviewer asking follow-ups while you code. You get a scored report at the end.

Practice this as a mock interviewFree to start, no account needed

Problem

Each node of a singly linked list holds a single bit, 0 or 1. Reading the list from head to tail gives the binary representation of a non-negative integer, with the most significant bit first. Return that integer in decimal. Leading zero bits are allowed and do not change the value. The list has at most 30 nodes, so the answer fits in a 32-bit signed integer.

Examples

Input: head = [1,0,1]
Output: 5
Binary 101 is 4 + 0 + 1 = 5.
Input: head = [0]
Output: 0
A single zero bit is 0.

Constraints

  • 1 <= number of nodes <= 30
  • Node.val is 0 or 1

What the interviewer grades

A correct solution is one of five scores. The report also grades problem understanding (did you clarify inputs and edge cases before coding), problem solving, communication while you work, code quality, and testing. See a sample report, or read up on the pattern in the lessons.

More bit manipulation problems