Convert Binary Number in a Linked List to Integer - Solution & Explanation
Problem Statement
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.
Example 1:
Input: head = [1,0,1] Output: 5 Explanation: (101) in base 2 = (5) in base 10
Example 2:
Input: head = [0] Output: 0
Constraints:
- The Linked List is not empty.
- Number of nodes will not exceed
30. - Each node's value is either
0or1.
Approach Overview
Problem Overview: A singly linked list stores a binary number where each node contains either 0 or 1. The head represents the most significant bit. Traverse the list and convert this binary sequence into its decimal integer value.
Approach 1: Bit Manipulation (O(n) time, O(1) space)
This approach treats the traversal like constructing a binary number bit by bit. Start with result = 0. For every node, shift the current value left by one (result << 1) and add the nodeβs bit using | node.val or simple addition. Each iteration effectively multiplies the current value by two and appends the new bit. The algorithm performs a single pass through the linked list, making it the most direct and efficient solution. Time complexity is O(n) and space complexity is O(1).
Approach 2: Reverse Iteration and Power of Two (O(n) time, O(n) space)
This method first collects all node values into an array while iterating through the list. After that, iterate the array from right to left and compute the decimal value using powers of two. For each bit at index i, add bit * 2^position to the result. The logic mirrors standard binary-to-decimal conversion using math. While still linear time, this approach uses extra memory to store the bits before computing the final value. Time complexity is O(n) and space complexity is O(n).
Recommended for interviews: The Bit Manipulation approach is the expected solution. It processes the list in one pass with constant space and clearly demonstrates understanding of binary operations. The power-of-two approach works but adds unnecessary memory overhead. Interviewers typically want to see how you apply bit manipulation while traversing a linked list.
Approach 1: Bit Manipulation
This approach involves traversing the linked list and building the decimal value using bit manipulation. As the linked list is traversed, the current result is shifted left by one bit (equivalent to multiplying by 2), and the current node's value is added using the OR operation.
The function getDecimalValue traverses the linked list, updating the result by left-shifting it by one bit and then OR-ing it with the current node's value. This operation effectively constructs the binary number bit by bit.
Complexity
Time Complexity: O(n), where n is the number of nodes in the linked list.
Space Complexity: O(1), as we only use a constant amount of additional space.
Approach 2: Reverse Iteration and Power of Two
This approach involves traversing the linked list twice. First, to count the nodes (and hence determine the power of two to start with) and second to compute the decimal value by iterating through the list again, multiplying each binary digit by the appropriate power of two.
In this implementation, we traverse the list to calculate the length, which aids in determining the highest power of two needed. A second pass through the list allows for calculation of the decimal value, multiplying each node value by 2 raised to the correct power.
Complexity
Time Complexity: O(n), where n is the number of nodes, but involves two passes.
Space Complexity: O(1), since it uses constant space plus a few variables.
Approach 3: Traverse the Linked List
We use a variable ans to record the current decimal value, with an initial value of 0.
Traverse the linked list. For each node, left-shift ans by one bit, then perform a bitwise OR with the current node's value. After traversal, ans is the decimal value.
The time complexity is O(n), where n is the length of the linked list. The space complexity is O(1).
Code
Python
Java
C++
Go
TypeScript
Rust
JavaScript
C#
PHP
C
Complexity Comparison
| Approach | Complexity |
|---|---|
| Bit Manipulation | Time Complexity: O(n), where n is the number of nodes in the linked list. |
| Reverse Iteration and Power of Two | Time Complexity: O(n), where n is the number of nodes, but involves two passes. |
| Traverse the Linked List | β |
Detailed Complexity Analysis
| Approach | Time | Space | When to Use |
|---|---|---|---|
| Bit Manipulation | O(n) | O(1) | Best general solution. Single pass through the linked list with constant memory. |
| Reverse Iteration and Power of Two | O(n) | O(n) | Useful for learning binary-to-decimal conversion or when storing nodes in an array for later processing. |
Video Solution
Convert Binary Number in a Linked List to Integer | LeetCode 1290 | C++, Java, Python β’ Knowledge Center β’ 14,820 views views
Watch 9 more video solutions βFrequently Asked Questions
Is Convert Binary Number in a Linked List to Integer easy or hard?
Convert Binary Number in a Linked List to Integer Python/Java solution
How to solve Convert Binary Number in a Linked List to Integer in O(n)?
What is the best approach for Convert Binary Number in a Linked List to Integer?
Is Convert Binary Number in a Linked List to Integer asked at Google/Amazon/Meta?
What data structure is used in Convert Binary Number in a Linked List to Integer?
What is the time complexity of Convert Binary Number in a Linked List to Integer?
Ready to solve this problem?
Practice Convert Binary Number in a Linked List to Integer with our built-in code editor and test cases.
Practice on FleetCode