Decimal Equivalent of Binary Linked List

Last Updated : 7 Sep, 2026

Given a singly linked list where each node contains either 0 or 1. The linked list represents a binary number, where the head node is the most significant bit (MSB). 

Convert this binary number into its decimal equivalent. If the linked list is empty, it represents the number 0.

Since the result can be very large, return the answer modulo 109 + 7.

Input: LinkedList: 1 -> 1 -> 1 -> 0

973

Output: 14
Explanation: 1 * 23 + 1 * 22 + 1 * 21 + 0 * 20 =  8 + 4 + 2 + 0 = 14.

Input: LinkedList: 0 -> 1 -> 1

974

Output: 3
Explanation: 0 * 22 + 1 * 21 + 1 * 20 =  1 + 2 + 0 = 3.

Try It Yourself
redirect icon

[Naive Approach] Using Two Traversals - O(n) Time and O(1) Space

The idea is to use two traversals of the linked list. In the first traversal, find the length of the linked list. Then, in the second traversal, assign each bit its corresponding power of 2, starting from 2^(n-1) for the MSB and decreasing the power by 1 at every node.

  • Traverse the linked list once and find its length n.
  • Calculate the highest power of 2, 2^(n-1), modulo 10^9 + 7.
  • Traverse the list again from the head.
  • For every node, add node->data × power to the result.
  • Divide power by 2 for the next bit and apply modulo at every step.
  • Return the final result modulo 10^9 + 7.
C++
#include <bits/stdc++.h>
using namespace std;

class Node
{
  public:
    int data;
    Node *next;

    Node(int x)
    {
        data = x;
        next = nullptr;
    }
};

// Returns the decimal value of the binary linked list.
int decimalValue(Node *head)
{
    long long MOD = 1000000007;

    // Modulo inverse of two, using fermat's little theorem
    long long INV2 = 500000004;

    // First traversal: find the length of the linked list.
    int n = 0;
    Node *temp = head;

    while (temp != nullptr)
    {
        n++;
        temp = temp->next;
    }

    // Empty linked list represents 0.
    if (n == 0)
        return 0;

    // Calculate 2^(n-1) modulo MOD.
    long long power = 1;

    for (int i = 1; i <= n - 1; i++)
    {
        power = (power * 2) % MOD;
    }

    // Stores the decimal value.
    long long res = 0;

    // Second traversal: calculate the positional value
    // of each binary bit.
    temp = head;

    while (temp != nullptr)
    {
        // Add current bit multiplied by its power of 2.
        res = (res + temp->data * power) % MOD;

        // Move to the next lower power of 2.
        // Multiply by modular inverse of 2 instead of
        // directly dividing by 2.
        power = (power * INV2) % MOD;

        temp = temp->next;
    }

    return res;
}

int main()
{
    // Binary number: 1011
    Node *head = new Node(1);
    head->next = new Node(0);
    head->next->next = new Node(1);
    head->next->next->next = new Node(1);

    cout << decimalValue(head) << endl;

    return 0;
}
Java
import java.util.*;

class Node {
    int data;
    Node next;

    Node(int x)
    {
        data = x;
        next = null;
    }
}

class GFG {
    static final long MOD = 1000000007;
    
    // Modulo inverse of two, using fermat's little theorem
    static final long INV2 = 500000004;

    // Returns the decimal value of the binary linked list.
    static int decimalValue(Node head)
    {
        // First traversal: find the length of the linked
        // list.
        int n = 0;
        Node temp = head;

        while (temp != null) {
            n++;
            temp = temp.next;
        }

        // Empty linked list represents 0.
        if (n == 0)
            return 0;

        // Calculate 2^(n-1) modulo MOD.
        long power = 1;

        for (int i = 1; i <= n - 1; i++) {
            power = (power * 2) % MOD;
        }

        // Stores the decimal value.
        long res = 0;

        // Second traversal: calculate the positional value
        // of each binary bit.
        temp = head;

        while (temp != null) {

            // Add current bit multiplied by its power of 2.
            res = (res + temp.data * power) % MOD;

            // Move to the next lower power of 2.
            // Multiply by modular inverse of 2 instead of
            // directly dividing by 2.
            power = (power * INV2) % MOD;

            temp = temp.next;
        }

        return (int)res;
    }

    public static void main(String[] args)
    {
        // Binary number: 1011
        Node head = new Node(1);
        head.next = new Node(0);
        head.next.next = new Node(1);
        head.next.next.next = new Node(1);

        System.out.println(decimalValue(head));
    }
}
Python
class Node:
    def __init__(self, x):
        self.data = x
        self.next = None


# Returns the decimal value of the binary linked list.
def decimalValue(head):

    MOD = 1000000007

    # Modulo inverse of two, using fermat's little theorem
    INV2 = 500000004

    # First traversal: find the length of the linked list.
    n = 0
    temp = head

    while temp is not None:
        n += 1
        temp = temp.next

    # Empty linked list represents 0.
    if n == 0:
        return 0

    # Calculate 2^(n-1) modulo MOD.
    power = 1

    for i in range(1, n):
        power = (power * 2) % MOD

    # Stores the decimal value.
    res = 0

    # Second traversal: calculate the positional value
    # of each binary bit.
    temp = head

    while temp is not None:
        # Add current bit multiplied by its power of 2.
        res = (res + temp.data * power) % MOD

        # Move to the next lower power of 2.
        # Multiply by modular inverse of 2 instead of
        # directly dividing by 2.
        power = (power * INV2) % MOD

        temp = temp.next

    return res


# Driver Code
if __name__ == "__main__":

    # Binary number: 1011
    head = Node(1)
    head.next = Node(0)
    head.next.next = Node(1)
    head.next.next.next = Node(1)

    print(decimalValue(head))
C#
using System;

class Node {
    public int data;
    public Node next;

    public Node(int x)
    {
        data = x;
        next = null;
    }
}

class GFG {
    const long MOD = 1000000007;

    // Returns the decimal value of the binary linked list.
    public static int decimalValue(Node head)
    {
        // First traversal: find the length of the linked
        // list.
        int n = 0;
        Node temp = head;

        while (temp != null) {
            n++;
            temp = temp.next;
        }

        // Empty linked list represents 0.
        if (n == 0)
            return 0;

        // Calculate 2^(n-1) modulo MOD.
        long power = 1;

        for (int i = 1; i <= n - 1; i++) {
            power = (power * 2) % MOD;
        }

        // Stores the decimal value.
        long res = 0;

        // Second traversal: calculate the positional value
        // of each binary bit.
        temp = head;

        while (temp != null) {
            // Add current bit multiplied by its power of 2.
            res = (res + temp.data * power) % MOD;

            // Move to the next lower power of 2.
            power = power / 2;

            temp = temp.next;
        }

        return (int)res;
    }

    public static void Main()
    {
        // Binary number: 1011
        Node head = new Node(1);
        head.next = new Node(0);
        head.next.next = new Node(1);
        head.next.next.next = new Node(1);

        Console.WriteLine(decimalValue(head));
    }
}
JavaScript
class Node {
    constructor(x)
    {
        this.data = x;
        this.next = null;
    }
}

// Returns the decimal value of the binary linked list.
function decimalValue(head)
{
    const MOD = 1000000007n;

    // Modulo inverse of two, using fermat's little theorem
    const INV2 = 500000004n;

    // First traversal: find the length of the linked list.
    let n = 0;
    let temp = head;

    while (temp !== null) {
        n++;
        temp = temp.next;
    }

    // Empty linked list represents 0.
    if (n === 0)
        return 0;

    // Calculate 2^(n-1) modulo MOD.
    let power = 1n;

    for (let i = 1; i <= n - 1; i++) {
        power = (power * 2n) % MOD;
    }

    // Stores the decimal value.
    let res = 0n;

    // Second traversal: calculate the positional value
    // of each binary bit.
    temp = head;

    while (temp !== null) {

        // Add current bit multiplied by its power of 2.
        res = (res + BigInt(temp.data) * power) % MOD;

        // Move to the next lower power of 2.
        // Multiply by modular inverse of 2 instead of
        // directly dividing by 2.
        power = (power * INV2) % MOD;

        temp = temp.next;
    }

    return Number(res);
}

// Driver Code

// Binary number: 1011
let head = new Node(1);
head.next = new Node(0);
head.next.next = new Node(1);
head.next.next.next = new Node(1);

console.log(decimalValue(head));

Output
11

[Expected Approach] Using Single Traversal - O(n) Time and O(1) Space

The idea is to use the property of binary numbers that appending a bit is equivalent to multiplying the current value by 2 and adding the new bit.

  • Initialize res = 0.
  • Traverse the linked list from the head.
  • For each node, update res = (res × 2 + node->data) % MOD.
  • Move to the next node.
  • Continue until the list becomes empty and finally, return res.

Consider the following example for better understanding: 1 --> 0 --> 1 --> 1

Initialize res = 0.

  • For 1: res = 0 × 2 + 1 = 1
  • For 0: res = 1 × 2 + 0 = 2
  • For 1: res = 2 × 2 + 1 = 5
  • For 1: res = 5 × 2 + 1 = 11

So, the answer is 11.

C++
#include <iostream>
using namespace std;

class Node
{
  public:
    int data;
    Node *next;

    Node(int val)
    {
        data = val;
        next = nullptr;
    }
};

int decimalValue(Node *head)
{
    const long long MOD = 1000000007;

    // Stores the decimal value of the binary number.
    long long res = 0;

    // Traverse the linked list.
    while (head != nullptr)
    {
        // Multiply the current value by 2 and
        // add the current binary digit.
        res = (res * 2 + head->data) % MOD;

        // Move to the next node.
        head = head->next;
    }

    // Return the final decimal value.
    return res;
}

int main()
{
    // Binary number: 1011
    Node *head = new Node(1);
    head->next = new Node(0);
    head->next->next = new Node(1);
    head->next->next->next = new Node(1);

    cout << decimalValue(head) << endl;

    return 0;
}
Java
class Node {
    int data;
    Node next;

    Node(int val)
    {
        data = val;
        next = null;
    }
}

class GFG {
    public static int decimalValue(Node head)
    {
        final long MOD = 1000000007;

        // Stores the decimal value of the binary number.
        long res = 0;

        // Traverse the linked list.
        while (head != null) {
            
            // Multiply the current value by 2 and
            // add the current binary digit.
            res = (res * 2 + head.data) % MOD;

            // Move to the next node.
            head = head.next;
        }

        // Return the final decimal value.
        return (int)res;
    }

    public static void main(String[] args)
    {
        // Binary number: 1011
        Node head = new Node(1);
        head.next = new Node(0);
        head.next.next = new Node(1);
        head.next.next.next = new Node(1);

        System.out.println(decimalValue(head));
    }
}
Python
class Node:
    def __init__(self, val):
        self.data = val
        self.next = None


def decimalValue(head):
    MOD = 1000000007

    # Stores the decimal value of the binary number.
    res = 0

    # Traverse the linked list.
    while head is not None:
        # Multiply the current value by 2 and
        # add the current binary digit.
        res = (res * 2 + head.data) % MOD

        # Move to the next node.
        head = head.next

    # Return the final decimal value.
    return res


# Driver Code
if __name__ == "__main__":
    
    # Binary number: 1011
    head = Node(1)
    head.next = Node(0)
    head.next.next = Node(1)
    head.next.next.next = Node(1)

    print(decimalValue(head))
C#
using System;

class Node {
    public int data;
    public Node next;

    public Node(int val)
    {
        data = val;
        next = null;
    }
}

class GFG {
    public static int decimalValue(Node head)
    {
        const long MOD = 1000000007;

        // Stores the decimal value of the binary number.
        long res = 0;

        // Traverse the linked list.
        while (head != null) {
            
            // Multiply the current value by 2 and
            // add the current binary digit.
            res = (res * 2 + head.data) % MOD;

            // Move to the next node.
            head = head.next;
        }

        // Return the final decimal value.
        return (int)res;
    }

    public static void Main()
    {
        // Binary number: 1011
        Node head = new Node(1);
        head.next = new Node(0);
        head.next.next = new Node(1);
        head.next.next.next = new Node(1);

        Console.WriteLine(decimalValue(head));
    }
}
JavaScript
class Node {
    constructor(val)
    {
        this.data = val;
        this.next = null;
    }
}

function decimalValue(head)
{
    const MOD = 1000000007n;

    // Stores the decimal value of the binary number.
    let res = 0n;

    // Traverse the linked list.
    while (head !== null) {
        // Multiply the current value by 2 and
        // add the current binary digit.
        res = (res * 2n + BigInt(head.data)) % MOD;

        // Move to the next node.
        head = head.next;
    }

    // Return the final decimal value.
    return Number(res);
}

// Driver Code

// Binary number: 1011
let head = new Node(1);
head.next = new Node(0);
head.next.next = new Node(1);
head.next.next.next = new Node(1);

console.log(decimalValue(head));

Output
11
Comment