384. Convert Binary Number in Linked List to Integer

MediumLinked ListLinked ListBit Manipulation

Given the head of a linked list where each node holds a single binary digit (0 or 1), the list represents a binary number with the most significant bit at the head. Return its decimal value.

Input: An array of binary digits (MSB first).

Output: Integer — the decimal value.

Examples

Example 1
Input: [1,0,1]
Output: 5
Explanation: Binary 101 = 5.
Example 2
Input: [0]
Output: 0
Explanation: Zero.
Example 3
Input: [1,1,1,1]
Output: 15
Explanation: Binary 1111 = 15.

Constraints

Asked by

OracleGoogleMicrosoftAmazonBloombergMeta
Solve this problem in the editor →