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: 1234321
💡 Note: Long binary sequence converts to decimal 1234321

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
Binary Linked List → Decimal ConversionINPUT1head01nullPROCESSBinary: 1 × 2² + 0 × 2¹ + 1 × 2⁰= 1 × 4 + 0 × 2 + 1 × 1= 4 + 0 + 1 = 5OUTPUT5decimal
Understanding the Visualization
1
Input
Linked list with binary digits: [1,0,1]
2
Process
Convert binary 101 to decimal using positional values
3
Output
Return decimal value: 5
Key Takeaway
🎯 Key Insight: Each bit contributes to the final value based on its position - multiply by 2 and add the current bit
Asked in
Microsoft 15 Amazon 12 Apple 8 Google 6
187.0K Views
Medium Frequency
~15 min Avg. Time
2.2K Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen