Friday, January 23, 2026
HomeData Modelling & AIJava Program To Find Decimal Equivalent Of Binary Linked List

Java Program To Find Decimal Equivalent Of Binary Linked List

Given a singly linked list of 0s and 1s find its decimal equivalent.

Input: 0->0->0->1->1->0->0->1->0
Output: 50   

Input: 1->0->0
Output: 4

The decimal value of an empty linked list is considered as 0.

Initialize the result as 0. Traverse the linked list and for each node, multiply the result by 2 and add the node’s data to it.

Java




// Java Program to find decimal value
// of binary linked list
class GFG{
     
// Link list Node
static class Node
{
    boolean data;
    Node next;
};
 
// Returns decimal value of binary
// linked list
static int decimalValue(Node head)
{
    // Initialized result
    int res = 0;
 
    // Traverse linked list
    while (head != null)
    {
        // Multiply result by 2 and
        // add head's data
        res = (res << 1) + (head.data?1:0);
 
        // Move next
        head = head.next;
    }
    return res;
}
 
// Utility function to create a
// new node.
static Node newNode(int data)
{
    Node temp = new Node();
    temp.data = (data == 1 ?
                 true : false);
    temp.next = null;
    return temp;
}
 
// Driver code
public static void main(String args[])
{
    // Start with the empty list
    Node head = newNode(1);
    head.next = newNode(0);
    head.next.next = newNode(1);
    head.next.next.next = newNode(1);
    System.out.print("Decimal value is " +
                      decimalValue(head));
}
}
// This code is contributed by Arnab Kundu


Output : 

Decimal value is 11

Time Complexity: O(n) where n is the number of nodes in the given linked list.
Auxiliary Space: O(1), no extra space is required, so it is a constant.

Please refer complete article on Decimal Equivalent of Binary Linked List for more details!

Last Updated :
22 Jun, 2022
Like Article
Save Article


Previous

<!–

8 Min Read | Java

–>


Next


<!–

8 Min Read | Java

–>

RELATED ARTICLES

Most Popular

Dominic
32475 POSTS0 COMMENTS
Milvus
119 POSTS0 COMMENTS
Nango Kala
6847 POSTS0 COMMENTS
Nicole Veronica
11977 POSTS0 COMMENTS
Nokonwaba Nkukhwana
12065 POSTS0 COMMENTS
Shaida Kate Naidoo
6986 POSTS0 COMMENTS
Ted Musemwa
7221 POSTS0 COMMENTS
Thapelo Manthata
6934 POSTS0 COMMENTS
Umr Jansen
6912 POSTS0 COMMENTS