Skip to content
← All posts

Convert Binary Number in a Linked List to Integer

4 min read
leetcodeproblemeasylinked-listsmath

Convert Binary Number in a Linked List to Integer is LeetCode problem 1290. You are given a singly linked list where each node holds a single binary digit, either 0 or 1. The head of the list is the most significant bit (MSB), and the tail is the least significant bit (LSB). Your job is to return the decimal (base-10) integer that this binary number represents.

For example, a list 1 -> 0 -> 1 represents the binary number 101, which equals 5 in decimal.

101nullBinary: 101= 1 × 4 + 0 × 2 + 1 × 1 = 5 in decimal
The linked list stores binary digits from MSB to LSB. Head is the most significant bit.

Why this problem matters

This is rated Easy, and the solution is short. But it teaches you a technique that shows up in many binary and linked list problems: processing digits from most significant to least significant without knowing how many digits there are ahead of time.

With an array, you could read the length first, then use powers of 2. But with a linked list, you do not know the length until you have traversed the whole thing. You need a way to build the number as you go, one bit at a time.

The approach: shift and add

The key insight is that you can build a binary number from left to right using this formula at each node:

result = result * 2 + node.val

Here is why this works. Multiplying by 2 is the same as a left shift in binary. It moves all existing bits one position to the left, making room for the new bit. Then adding node.val places the new bit in the ones position.

Think of it like reading a decimal number left to right. If you have already read "12" and the next digit is "3", you compute 12 * 10 + 3 = 123. The same logic applies here, just with base 2 instead of base 10.

This is equivalent to the bitwise operation result = (result << 1) | node.val, but the multiplication form is clearer and works the same way.

The solution

class ListNode:
    def __init__(self, val=0, next=None):
        self.val = val
        self.next = next

def get_decimal_value(head: ListNode) -> int:
    result = 0
    while head:
        result = result * 2 + head.val
        head = head.next
    return result

That is the entire solution. Three lines inside the function. Start with result = 0, walk the list, and at each node shift left and add the current bit. When you reach the end, result holds the decimal value.

Visual walkthrough

Let us trace through the list 1 -> 0 -> 1 step by step. Watch how the result accumulates as we visit each node.

Step 1: Visit node with value 1

101nullhead

result = 0 × 2 + 1 = 1

Start with result = 0. Multiply by 2 and add the current bit.

Step 2: Visit node with value 0

101nullhead

result = 1 × 2 + 0 = 2

Shifting left in binary: 1 becomes 10 (decimal 2). The 0 bit adds nothing.

Step 3: Visit node with value 1

101nullhead

result = 2 × 2 + 1 = 5

Final shift and add: 10 becomes 101 (decimal 5). We have our answer.

Notice how the result doubles at each step before adding the new bit. After processing all three nodes, we arrive at 5, which is the decimal value of binary 101.

Complexity analysis

MetricValueWhy
TimeO(n)We visit each node exactly once
SpaceO(1)We only use a single integer variable

You cannot do better than O(n) time because you must read every node to know the full binary number. And O(1) space is optimal since we only track one running total.

Edge cases to watch for

  • Single node. The list has one node with value 0 or 1. The result is just that value. The loop runs once: 0 * 2 + val = val.
  • All zeros. A list like 0 -> 0 -> 0 produces 0. Each step doubles 0 and adds 0.
  • All ones. A list like 1 -> 1 -> 1 produces 7 (binary 111). Each step doubles and adds 1: 0 to 1, 1 to 3, 3 to 7.
  • Leading zeros. A list like 0 -> 1 -> 0 correctly produces 2 (binary 010). The initial zeros naturally do not affect the result since doubling 0 stays 0.

The building blocks

This problem is built from two core building blocks:

Bit manipulation with shift-and-add. The technique of building a number by repeatedly multiplying by the base and adding the next digit. This is the same pattern you use when parsing any number from a string or stream of digits. You will see it again in problems that involve binary representations, base conversions, or constructing numbers digit by digit.

Linked list traversal. Walking a linked list from head to tail, processing each node exactly once. This is the most fundamental linked list operation. If you are comfortable with the while head: ... head = head.next pattern, you can apply it to dozens of other problems.

The result * 2 + bit pattern is one of those building blocks worth memorizing. It works for any base, not just binary. For base 10, use result * 10 + digit. For base 16, use result * 16 + digit. Same idea, different multiplier.

From understanding to recall

You understand this problem now. The formula is simple, the code is short, and the logic is clear. But will you remember it in two weeks when a similar problem appears?

The challenge with easy problems is not understanding them. It is having instant recall when you need the technique under pressure. You might hesitate on whether to multiply by 2 before or after adding the bit. You might second-guess whether to start with result = 0 or result = head.val.

The fix is repetition. Type the solution from scratch a few times over the next week. After three or four reps, the pattern becomes automatic. You will not need to rederive it during an interview.

Related posts

  • Reverse Linked List - The classic pointer manipulation problem that every linked list solution builds on
  • Add Two Numbers - Another problem that processes linked list nodes as digits of a number, but in reverse order
  • Palindrome Linked List - Combines linked list traversal with a different kind of value comparison