Convert Binary Number in a Linked List to Integer - Complete Solution Guide
Convert Binary Number in a Linked List to Integer is LeetCode problem 1290, a Easy level challenge. This complete guide provides step-by-step explanations, multiple solution approaches, and optimized code in python3, java, cpp, c.
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 e
Detailed Explanation
You're presented with a singly-linked list where each node's `val` is strictly `0` or `1`. This linked list isn't just holding arbitrary digits; it's representing a binary number. Your objective is to convert this binary representation into its equivalent decimal (base-10) integer value. A crucial piece of information is that the `head` of the linked list contains the *most significant bit* (MSB) of the binary number, meaning the bits are ordered from left to right, just like you'd read a binary number normally.
Solution Approach
The provided solution leverages a concise and highly efficient iterative approach. It initializes a `num` variable to `0`, which will progressively accumulate the decimal value. The algorithm then traverses the linked list from the head to the tail. For each node's `head.val` (which is either `0` or `1`), it performs a critical operation: `num = (num << 1) | head.val`. Let's break down this operation: `num << 1` performs a bitwise left shift on `num`. This effectively multiplies `num` by `2` and, importantly, shifts all its existing bits one position to the left, making the least significant bit (LSB) position `0`. For example, if `num` was binary `101` (decimal `5`), `num << 1` makes it binary `1010` (decimal `10`). Once this shift creates a `0` at the LSB, `| head.val` performs a bitwise OR operation. Since `head.val` is either `0` or `1`, this operation effectively 'appends' `head.val` into that LSB slot. If `head.val` is `0`, the LSB remains `0`. If `head.val` is `1`, the LSB becomes `1`. This perfectly simulates appending the current binary digit to the right of the growing binary number, continuously updating its decimal equivalent. This process repeats for every node until the list is exhausted, at which point `num` holds the final decimal value.
Step-by-Step Algorithm
- Step 1: Initialize an integer variable `decimalValue` to 0. This variable will store the accumulating decimal value.
- Step 2: Initialize a pointer (e.g., `current`) to the head of the linked list.
- Step 3: While the `current` pointer is not null (end of the list):
- Step 4: Perform a left bit shift on `decimalValue` by 1 bit: `decimalValue = decimalValue << 1`
- Step 5: Perform a bitwise OR operation between `decimalValue` and the current node's value: `decimalValue = decimalValue | current.val`
- Step 6: Move the `current` pointer to the next node: `current = current.next`
- Step 7: Return the final value of `decimalValue`.
Key Insights
- **Iterative MSB-to-LSB Binary Conversion:** The core technique is to build the decimal number by processing binary digits from MSB to LSB. For each new bit encountered, the running total is effectively doubled (shifted left) and then the current bit's value is added. This avoids the need to store all bits or explicitly calculate powers of two.
- **Efficient Bitwise Operations:** The `(num << 1) | head.val` pattern is the cornerstone. `<< 1` (left shift by one) is a super-fast way to multiply by 2, preparing space for the next bit. `|` (bitwise OR) then neatly inserts the current bit (`0` or `1`) into the newly vacant least significant position. This combination is a standard, highly performant idiom for constructing an integer from a stream of bits.
- **Single Pass, Constant Space:** The solution processes the linked list in a single pass, accumulating the result directly into an integer variable. This achieves optimal time complexity of O(N) where N is the number of nodes, and optimal space complexity of O(1) (excluding the input linked list itself), making it very efficient for potentially large binary numbers.
Complexity Analysis
Time Complexity: O(n)
Space Complexity: O(1)
Topics
This problem involves: Linked List, Math.
Companies
Asked at: MathWorks, Roblox, Workday.