  Get free ebooK with 50 must do coding Question for Product Based Companies solved
Fill the details & get ebook over email  Thank You!
We have sent the Ebook on 50 Must Do Coding Questions for Product Based Companies Solved over your email. All the best!

# Decimal Equivalent of Binary Linked List

Last Updated on November 16, 2022 by Prepbytes In this article, we will discuss the decimal equivalent of a binary linked list. For converting a number from binary to a decimal using the positional notation method, we multiply each digit of the binary number with its base, (which is 2), raised to the power based on its position in the binary number. Practicing a linked list plays a very important role in the interviews. Having a good grasp of Linked Lists can be a huge plus point in a coding interview.

### Problem Statement

We will be given a Binary linked list, i.e, a list where each node will be having either 0 or 1 as its data, and we need to find its decimal equivalent.

### Problem Statement Understanding How To Obtain Decimal Equivalent Of Binary Linked List

Let’s try to understand the problem with the help of an example:

If the linked list given to us is 1→1→0→1→NULL

• According to the problem statement, we need to find the decimal equivalent of the given binary list.
• We can see that the binary equivalent of the given list is 1101.
• Now, on converting this binary equivalent 1101 into decimal, we get 13.
• So, the output will be 13.

Let’s take another example.
If the linked list given to us is 1→1→0→1→0→1→NULL.

• In this case, the binary equivalent of the given list is 110101.
• Now, on converting the binary equivalent 110101 into decimal, we get 53.
• So, the output will be 53.

At this point, we have understood the problem statement. Now we will try to formulate an approach to this problem.

Before moving to the approach section, try to think about how you can approach this problem.

• If stuck, no problem, we will thoroughly see how we can approach this problem in the next section.

Let’s move to the approach section.

### Approach For Converting Into Decimal Equivalent Of Binary Linked List

Our approach will be straightforward:

• We will iterate the list from starting and accumulate the result in a variable.
• While we move through the list, the position of binary numbers that have been encountered till now in our result shifts by one to the left.
• So that’s why we will multiply our result by 21 (as there is a shift by one position) and add the current position’s data.
• Finally, we will be left with a decimal number, which will be the final decimal representation of our binary list.

### Algorithm For Converting Into Decimal Equivalent Of Binary Linked List

• Initialize an ans variable by 0 and a curr pointer by the head of the list.
• Run a while loop till the curr pointer is not NULL.
• Every time inside the loop, multiply the ans by 2 and add the curr->data in ans.
• Move the curr pointer by one node.
• Return the ans after the loop breaks.

### Dry Run For Converting Into Decimal Equivalent Of Binary Linked List  ### Code Implementation Of Obtaining Decimal Equivalent Of Binary Linked List

```#include<stdio.h>
#include<stdlib.h>
#include<stdbool.h>

struct Node
{
bool data;
struct Node* next;
};

/* Returns decimal value of binary linked list */
{
// Initialized result
int  res = 0;

{
// Multiply result by 2 and add
res = (res  << 1) + head->data;

// Move next
}
return res;
}

// Utility function to create a new node.
struct Node *newNode(bool data)
{
struct Node *temp =(struct Node *)malloc(sizeof(struct Node));
temp->data = data;
temp->next = NULL;
return temp;
}

/* Driver program to test above function*/
int main()
{
printf("Decimal value is %d ",ans);

return 0;
}
```
```#include <bits/stdc++.h>
using namespace std;

class Node {
public:
int data;
Node* next;
Node(int d)
{
data = d;
next = NULL;
}
};

// This function will return decimal
// equivalent of a binary list
{
// Initialize 'ans' by 0
int ans = 0;

// run a while loop till 'curr' is not NULL
while (curr != NULL)
{
// multiply the 'ans' by 2 and add
// 'curr' data to it
ans = (ans * 2) + curr->data;

// move 'curr' by one node
curr = curr->next;
}
return ans;
}

int main()
{
Node *list = new Node(1);
list->next = new Node(1);
list->next->next = new Node(0);
list->next->next->next = new Node(1);
cout<<"Decimal Equivalent of the given binary list is: ";
cout<<binaryToDecimal(list);
}

```
```class BinaryLinkedList
{

static class Node
{
boolean data;
Node next;
};

// Returns decimal value of binary linked list /
{
// Initialized result
int res = 0;

{
// Multiply result by 2 and add
res = (res << 1) + (head.data?1:0);

// Move 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[])
{

}
}
```
```class Node:

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

def __init__(self):

# this function will return decimal
# equivalent of a binary linked list

# Initialized result
res = 0

# Multiply result by 2 and
res = (res << 1) + head.data

# Move Next

return res

if __name__ == '__main__':

print("Decimal Equivalent of the given binary list is: {}".format(

```

### Output

Decimal Equivalent of the given binary list is: 13

Time Complexity For Getting Decimal Equivalent Of Binary Linked List: O(n), where n is the number of nodes in the linked list.