Convert Binary Number in a Linked List to Integer - Problem
Given head which is a reference node to a singly-linked list. The value of each node in the linked list is either 0 or 1. The linked list holds the binary representation of a number.
Return the decimal value of the number in the linked list. The most significant bit is at the head of the linked list.
Input & Output
Example 1 — Basic Binary Conversion
$
Input:
head = [1,0,1]
›
Output:
5
💡 Note:
Binary number 101₂ = 1×4 + 0×2 + 1×1 = 5 in decimal
Example 2 — Single Bit
$
Input:
head = [0]
›
Output:
0
💡 Note:
Single bit 0 represents decimal 0
Example 3 — Longer Binary Number
$
Input:
head = [1,0,0,1,0,0,1,1,1,0,0,0,1,0,0,1,1,0,0,1,0]
›
Output:
1208626
💡 Note:
Long binary sequence 100100111000100110010₂ converts to decimal 1208626
Constraints
- The linked list is not empty
- Number of nodes is in the range [1, 30]
- Each node's value is either 0 or 1
Visualization
Tap to expand
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code