# 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```

Decimal Value of an empty linked list is considered as 0.

## Recommended: Please solve it on “PRACTICE” first, before moving on to the solution.

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

## C++

 `// C++ Program to find decimal value of  ` `// binary linked list  ` `#include ` `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;  ` `}  ` ` `  `/* Drier 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

 `// C Program to find decimal value of ` `// binary linked list ` `#include ` `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; ` `} ` ` `  `/* Drier 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

 `// 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 `

## C#

 `// C# Program to find decimal value of  ` `// binary linked list  ` `using` `System; ` ` `  `class` `GFG ` `{ ` `     `  `// Link list Node / ` `public` `class` `Node  ` `{  ` `    ``public` `Boolean data;  ` `    ``public` `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 cod ` `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);  ` ` `  `    ``Console.WriteLine(``"Decimal value is "` `+  ` `                       ``decimalValue(head));  ` `} ` `} ` ` `  `// This code is contributed by Rajput-Ji `

Output :

`Decimal value is 11`

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 contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.