Problem 891
Convert Binary Number in a Linked List to Integer
Easy · 10 pts · Linked List, Math, Bit Manipulation
Problem
Each node of a linked list holds a bit, 0 or 1, and the list read from head to tail is a binary number with the most significant bit at the head. Print its value in decimal. Walk the list once, keeping value = value × 2 + bit; you never need to know the length of the list in advance. The list has at most 30 nodes, so the value fits in a 32-bit integer.
Input
The first line contains N. The second line contains the N bits from head to tail.
Output
Print the decimal value.
Constraints
1 <= N <= 30; every bit is 0 or 1
Example input
3 1 0 1
Example output
5
Explanation
The list 1 0 1 is the binary number 101, which is 4 + 0 + 1 = 5 in decimal.