Decimal Equivalent of Binary Linked List

Last Updated : 9 May, 2026

You are 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 :

file

Output : 50

Input :

file

Output : 4

Try It Yourself
redirect icon

[Optimal Approach] Using Bit Manipulation (Left Shift) – O(n) Time and O(1) Space

The linked list represents a binary number where each node is a bit (0 or 1). We can convert it into a decimal value by traversing the list and continuously updating the result. At each step, we multiply the current result by 2 (using left shift) and add the current node’s value, which mimics binary to decimal conversion efficiently.

  • Initialize res = 0 to store the result.
  • Traverse the linked list from head to end.
  • For each node: Left shift the result res = res << 1 (multiply by 2), add current node value res = res + head->data and take modulo 109+710^9 + 7109+7 to prevent overflow.
C++
#include <iostream>
using namespace std;


struct Node {
    int data;
    Node* next;

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

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

        while (head != nullptr) {
            res = ((res << 1) + head->data) % MOD;
            head = head->next;
        }

        return res;
    }

int main() {
    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);
    return 0;
}
C
#include <stdio.h>
#include <stdlib.h>

#define MOD 1000000007

// Node structure
struct Node {
    int data;
    struct Node* next;
};

// Create new node
struct Node* newNode(int val) {
    struct Node* node = (struct Node*)malloc(sizeof(struct Node));
    node->data = val;
    node->next = NULL;
    return node;
}

// Function to convert binary LL to decimal
int decimalValue(struct Node* head) {
    long long res = 0;

    while (head != NULL) {
        res = ((res << 1) + head->data) % MOD;
        head = head->next;
    }

    return (int)res;
}

int main() {
    struct Node* head = newNode(1);
    head->next = newNode(0);
    head->next->next = newNode(1);
    head->next->next->next = newNode(1);

    printf("%d", decimalValue(head));
    return 0;
}
Java
class GFG {

    static class Node {
        int data;
        Node next;

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

    static int decimalValue(Node head) {
        long res = 0;
        int MOD = 1000000007;

        while (head != null) {
            res = ((res << 1) + head.data) % MOD;
            head = head.next;
        }

        return (int)res;
    }

    public static void main(String[] args) {
        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
MOD = 1000000007

class Node:
    def __init__(self, val):
        self.data = val
        self.next = None

def decimalValue(head):
    res = 0

    while head:
        res = ((res << 1) + head.data) % MOD
        head = head.next

    return res


# Example
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 GFG {

    class Node {
        public int data;
        public Node next;

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

    static int DecimalValue(Node head) {
        long res = 0;
        int MOD = 1000000007;

        while (head != null) {
            res = ((res << 1) + head.data) % MOD;
            head = head.next;
        }

        return (int)res;
    }

    static void Main() {
        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
const MOD = 1000000007;

// Node class
class Node {
    constructor(val) {
        this.data = val;
        this.next = null;
    }
}

// Function to convert binary LL to decimal
function decimalValue(head) {
    let res = 0;

    while (head !== null) {
        res = ((res << 1) + head.data) % MOD;
        head = head.next;
    }

    return res;
}

// Example
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
Decimal value is 11

[Another Approach] Using Reverse + Power Calculation – O(n log n) Time and O(n) Space (Recursion Stack)

In this approach, the linked list is first reversed so that we can process bits from least significant bit (LSB) to most significant bit (MSB). Then, for each node, we calculate its contribution using the formula bit×2(position). A fast exponentiation (power) function is used to efficiently compute powers of 2 under modulo. By summing all contributions, we get the decimal value of the binary number.

  • Reverse the linked list to process from LSB to MSB.
  • Initialize ans = 0 and pos = 0.
  • Traverse the reversed list:
  • For each node, compute power(2, pos) for its position, multiply it with current node value (0 or 1) and add it to ans with modulo.
  • Increment position pos after each node.
C++
#include <bits/stdc++.h>
using namespace std;

// node structure
struct Node {
    int data;
    Node* next;

    Node(int data) {
        this->data = data;
        this->next = nullptr;
    }
};

// power function
long long unsigned int power(int num, int count) {
    if (count == 0) return 1;

    if (count % 2 == 0) {
        long long half = power(num, count / 2) % 1000000007;
        return (half * half) % 1000000007;
    } else {
        long long half = power(num, count / 2) % 1000000007;
        return (num * half % 1000000007 * half % 1000000007) % 1000000007;
    }
}

// reverse linked list
Node* reverse(Node* head) {
    if (head == nullptr || head->next == nullptr) return head;

    Node* curr = head;
    Node* prev = nullptr;
    Node* nxt = head->next;

    while (nxt!= nullptr) {
        curr->next = prev;
        prev = curr;
        curr = nxt;
        nxt = nxt->next;
    }

    curr->next = prev;
    return curr;
}

// function to get decimal value
int decimalValue(Node* head) {
    int MOD = 1000000007;

    Node* rhead = reverse(head);

    int ans = 0;
    int pos = 0;

    while (rhead!= nullptr) {
        ans = (ans % MOD + (rhead->data * power(2, pos)) % MOD) % MOD;
        rhead = rhead->next;
        pos++;
    }

    return ans;
}

// main function
int main() {
    Node* head = new Node(1);
    head->next = new Node(0);
    head->next->next = new Node(1);
    head->next->next->next = new Node(1);

    cout << "Decimal Value is : " << decimalValue(head);
}
Java
class GFG {

    static class Node {
        int data;
        Node next;

        Node(int data) {
            this.data = data;
            this.next = null;
        }
    }

    static final int MOD = 1000000007;

    // Fast power (binary exponentiation)
    static long power(int num, int count) {
        if (count == 0) return 1;

        long half = power(num, count / 2) % MOD;

        if (count % 2 == 0)
            return (half * half) % MOD;
        else
            return (num * half % MOD * half % MOD) % MOD;
    }

    // Reverse linked list
    static Node reverse(Node head) {
        Node prev = null, curr = head;

        while (curr != null) {
            Node next = curr.next;
            curr.next = prev;
            prev = curr;
            curr = next;
        }
        return prev;
    }

    // Convert to decimal
    static int decimalValue(Node head) {
        Node rhead = reverse(head);

        long ans = 0;
        int pos = 0;

        while (rhead != null) {
            ans = (ans + (rhead.data * power(2, pos)) % MOD) % MOD;
            rhead = rhead.next;
            pos++;
        }

        return (int) ans;
    }

    public static void main(String[] args) {
        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("Decimal Value is : " + decimalValue(head));
    }
}
Python
MOD = 1000000007

class Node:
    def __init__(self, data):
        self.data = data
        self.next = None

# Fast power
def power(num, count):
    if count == 0:
        return 1

    half = power(num, count // 2) % MOD

    if count % 2 == 0:
        return (half * half) % MOD
    else:
        return (num * half % MOD * half % MOD) % MOD

# Reverse linked list
def reverse(head):
    prev = None
    curr = head

    while curr:
        nxt = curr.next
        curr.next = prev
        prev = curr
        curr = nxt

    return prev

# Convert to decimal
def decimalValue(head):
    rhead = reverse(head)

    ans = 0
    pos = 0

    while rhead:
        ans = (ans + (rhead.data * power(2, pos)) % MOD) % MOD
        rhead = rhead.next
        pos += 1

    return ans


# Example
head = Node(1)
head.next = Node(0)
head.next.next = Node(1)
head.next.next.next = Node(1)

print("Decimal Value is:", decimalValue(head))
C#
using System;

class GFG {

    class Node {
        public int data;
        public Node next;

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

    static int MOD = 1000000007;

    // Fast power
    static long Power(int num, int count) {
        if (count == 0) return 1;

        long half = Power(num, count / 2) % MOD;

        if (count % 2 == 0)
            return (half * half) % MOD;
        else
            return (num * half % MOD * half % MOD) % MOD;
    }

    // Reverse linked list
    static Node Reverse(Node head) {
        Node prev = null, curr = head;

        while (curr != null) {
            Node next = curr.next;
            curr.next = prev;
            prev = curr;
            curr = next;
        }
        return prev;
    }

    // Convert to decimal
    static int DecimalValue(Node head) {
        Node rhead = Reverse(head);

        long ans = 0;
        int pos = 0;

        while (rhead != null) {
            ans = (ans + (rhead.data * Power(2, pos)) % MOD) % MOD;
            rhead = rhead.next;
            pos++;
        }

        return (int)ans;
    }

    static void Main() {
        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("Decimal Value is : " + DecimalValue(head));
    }
}
JavaScript
const MOD = 1000000007;

// Node class
class Node {
    constructor(data) {
        this.data = data;
        this.next = null;
    }
}

// Fast power
function power(num, count) {
    if (count === 0) return 1;

    let half = power(num, Math.floor(count / 2)) % MOD;

    if (count % 2 === 0)
        return (half * half) % MOD;
    else
        return (num * half % MOD * half % MOD) % MOD;
}

// Reverse linked list
function reverse(head) {
    let prev = null, curr = head;

    while (curr !== null) {
        let next = curr.next;
        curr.next = prev;
        prev = curr;
        curr = next;
    }
    return prev;
}

// Convert to decimal
function decimalValue(head) {
    let rhead = reverse(head);

    let ans = 0;
    let pos = 0;

    while (rhead !== null) {
        ans = (ans + (rhead.data * power(2, pos)) % MOD) % MOD;
        rhead = rhead.next;
        pos++;
    }

    return ans;
}

// Example
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("Decimal Value is :", decimalValue(head));

Output
Decimal Value is : 11
Comment