Convert Binary Number in Linked List

Easy

Description

Given head which is a reference node to a singly-linked list where node values are 0 or 1 (representing a binary number), return the decimal value of the number.

Examples

Input:head = [1,0,1]
Output:5
Explanation:

101 in binary = 5.

Input:head = [0]
Output:0
Explanation:

The linked list contains a single node with value 0, which represents binary 0 = decimal 0.

Input:head = [1,1,1,1]
Output:15
Explanation:

1111 in binary = 1×2³ + 1×2² + 1×2¹ + 1×2⁰ = 8 + 4 + 2 + 1 = 15.

Constraints

  • 1 ≤ nodes ≤ 30

Ready to solve this problem?

Practice solo or challenge other developers in a real-time coding battle!