Decimal Equivalent of Binary Linked List

01 Jul, 2022
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.
C++
C
Java
Python3
C#
Javascript
// C++ Program to find decimal value of// binary linked list#include <bits/stdc++.h>using namespace std; /* Link list Node */class Node{    public:    bool data;    Node* next;}; /* Returns decimal value of binary linked list */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;         // Move next        head = head->next;    }    return res;} // Utility function to create a new node.Node *newNode(bool data){    Node *temp = new Node;    temp->data = data;    temp->next = NULL;    return temp;} /* Driver program to test above function*/int main(){    /* Start with the empty list */    Node* head = newNode(1);    head->next = newNode(0);    head->next->next = newNode(1);    head->next->next->next = newNode(1);     cout << "Decimal value is "        << decimalValue(head);     return 0;} // This is code is contributed by rathbhupendra
// C Program to find decimal value of// binary linked list#include<iostream>using namespace std; /* Link list Node */struct Node{    bool data;    struct Node* next;}; /* Returns decimal value of binary linked list */int decimalValue(struct 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;         // Move next        head = head->next;    }    return res;} // Utility function to create a new node.Node *newNode(bool data){    struct Node *temp = new Node;    temp->data = data;    temp->next = NULL;    return temp;} /* Driver program to test above function*/int main(){    /* Start with the empty list */    struct Node* head = newNode(1);    head->next = newNode(0);    head->next->next = newNode(1);    head->next->next->next = newNode(1);     cout << "Decimal value is "         << decimalValue(head);     return 0;}
// Java Program to find decimal value of// binary linked listclass 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
# Python3 program to find decimal value# of binary linked list # Node Classclass Node:         # Function to initialise the    # node object    def __init__(self, data):                 # Assign data        self.data = data                 # Initialize next as null        self.next = None # Linked List class contains# a Node objectclass LinkedList:     # Function to initialize head    def __init__(self):                 self.head = None     # Returns decimal value of binary    # linked list    def decimalValue(self, head):                 # Initialized result        res = 0         # Traverse linked list        while head:             # Multiply result by 2 and            # add head's data            res = (res << 1) + head.data             # Move Next            head = head.next                     return res # Driver codeif __name__ == '__main__':     #Start with the empty list    llist = LinkedList()     llist.head = Node(1)    llist.head.next = Node(0)    llist.head.next.next = Node(1)    llist.head.next.next.next = Node(1)         print("Decimal Value is {}".format(           llist.decimalValue(llist.head))) # This code is contributed by Mohit Jangra
// C# Program to find decimal value of// binary linked listusing System; class GFG{     // Link list Node /public class Node{    public Boolean data;    public Node next;}; // Returns decimal value of binary linked liststatic 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 codpublic 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);     Console.WriteLine("Decimal value is " +                       decimalValue(head));}} // This code is contributed by Rajput-Ji
<script> // Javascript Program to find decimal value of// binary linked list    // Link list Node /     class Node {         constructor(){         this.data = true;         this.next = null;    }    }     // Returns decimal value of binary linked list /    function decimalValue(head) {        // Initialized result        var 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.    function newNode(data) {var temp = new Node();        temp.data = (data == 1 ? true : false);        temp.next = null;        return temp;    }     // Driver code/             // Start with the empty list /var head = newNode(1);        head.next = newNode(0);        head.next.next = newNode(1);        head.next.next.next = newNode(1);         document.write("Decimal value is "        + decimalValue(head)); // This code contributed by aashish1995 </script>
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.
Decimal Equivalent of Binary Linked List | GeeksforGeeks - YouTubeGeeksforGeeks531K subscribersDecimal Equivalent of Binary Linked List | GeeksforGeeksWatch laterShareCopy linkInfoShoppingTap to unmuteIf playback doesn't begin shortly, try restarting your device.You're signed outVideos you watch may be added to the TV's watch history and influence TV recommendations. To avoid this, cancel and sign in to YouTube on your computer.CancelConfirmMore videosMore videosSwitch cameraShareInclude playlistAn error occurred while retrieving sharing information. Please try again later.Watch on0:000:000:00 / 6:21•Live•<div class="player-unavailable"><h1 class="message">An error occurred.</h1><div class="submessage"><a href="https://www.youtube.com/watch?v=k9x5UjTYi5I" target="_blank">Try watching this video on www.youtube.com</a>, or enable JavaScript if it is disabled in your browser.</div></div>
This article is contributed by Shivam Gupta. If you like GeeksforGeeks and would like to contribute, you can also write an article and mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks. 
andrew1234
rathbhupendra
Rajput-Ji
Akanksha_Rai
itsmohitj
aashish1995
tamanna17122007
hardikkoriintern
Juniper Networks
Linked List
Juniper Networks
Linked List
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
LinkedList in Java
Introduction to Data Structures
Doubly Linked List | Set 1 (Introduction and Insertion)
Merge two sorted linked lists
What is Data Structure: Types, Classifications and Applications
Linked List vs Array
Merge Sort for Linked Lists
Implementing a Linked List in Java using Class
Find Length of a Linked List (Iterative and Recursive)
Queue - Linked List Implementation