Convert Binary Number in a Linked List to Integer (LeetCode 1290): Solution and Explanation

Easy
Linked List
Math
Bit Manipulation

The list stores a binary number with the most significant bit first. Walking it once and folding each bit in with num = num * 2 + bit gives the decimal value without knowing the length up front.

Intuition

Writing a digit after a number shifts the number one place left. In base 10 that multiplies by 10, and in base 2 it multiplies by 2. So appending bit b to a binary value x gives x * 2 + b.

That is exactly what reading the list from the head does: every node appends one more bit to the right. Each earlier bit's weight doubles at every step, so by the end the first bit carries 2^(n-1) and the last carries 1, which is the usual place value expansion.

Approach: Fold the bits from the head

  1. Set num = 0 and start at head.
  2. At each node, set num = num * 2 + node.val. Shifting with num << 1 | node.val is the same thing.
  3. Move to node.next and repeat until the list ends.
  4. Return num.

Step-by-step example

head = [1,0,1]
  1. num starts at 0.
  2. First node holds 1: num = 0 * 2 + 1 = 1.
  3. Second node holds 0: num = 1 * 2 + 0 = 2, which is binary 10.
  4. Third node holds 1: num = 2 * 2 + 1 = 5, which is binary 101.
  5. The list ends, so the answer is 5.

Code: Python, Java, C++

1class Solution:
2    def getDecimalValue(self, head: Optional[ListNode]) -> int:
3        num = 0
4        node = head
5        while node:
6            num = num * 2 + node.val
7            node = node.next
8        return num
Press play on the visualizer and the running line lights up here.

Time and space complexity

Time

O(n)

Each of the n nodes is visited once with constant work.

Space

O(1)

Only the running value and the node pointer are stored.

Other approaches

Two passes with place values (time O(n), space O(1))

Count the nodes first, then walk again adding node.val * 2^(n - 1 - i). It gives the same answer with an extra pass.

Build a string and parse it (time O(n), space O(n))

Collect the bits into a string and call the language's base 2 parser, such as int(s, 2). It is short but allocates a string and depends on library parsing.

Common mistakes

  • Treating the head as the least significant bit. The head is the most significant bit, so a left to right fold is needed, not powers that grow from the head.
  • Using num += node.val * 2^i with i counting from the head, which reverses the bit order.
  • Forgetting that leading zeros are allowed, as in [0,0,1,1,0], which simply keep num at 0 until the first 1.

Frequently asked questions

Why does multiplying by 2 work?

Multiplying a binary number by 2 shifts all of its bits one place left and leaves a 0 in the ones place. Adding the new bit fills that place, which is the same as writing the bit at the end of the number.

Can the result overflow a 32 bit int?

No. The list has at most 30 nodes, so the largest value is 2^30 - 1, which fits in a signed 32 bit integer.

Is there a bitwise version?

Yes. num = (num << 1) | node.val does the same work, since a left shift by one is multiplication by 2 and OR with a single bit into an empty ones place is addition.