
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
Convert Binary Number in a Linked List to Integer in C++
Suppose we have one ‘head’ which is a reference node to a singly-linked list. The value of each node present in the linked list is either 0 or 1. This linked list stores the binary representation of a number. We have to return the decimal value of the number present in the linked list. So if the list is like [1,0,1,1,0,1]
To solve this, we will follow these steps −
x := convert the list elements into an array
then reverse the list x
ans := 0, and temp := 1
for i in range i := 0, to size of x – 1
ans := ans + x[i] * temp
temp := temp * 2
return ans
Example (C++)
Let us see the following implementation to get a better understanding −
#include <bits/stdc++.h> using namespace std; class ListNode{ public: int val; ListNode *next; ListNode(int data){ val = data; next = NULL; } }; ListNode *make_list(vector<int> v){ ListNode *head = new ListNode(v[0]); for(int i = 1; i<v.size(); i++){ ListNode *ptr = head; while(ptr->next != NULL){ ptr = ptr->next; } ptr->next = new ListNode(v[i]); } return head; } class Solution { public: vector <int> getVector(ListNode* node){ vector <int> result; while(node){ result.push_back(node->val); node = node->next; } return result; } int getDecimalValue(ListNode* head) { vector <int> x = getVector(head); reverse(x.begin(), x.end()); int ans = 0; int temp = 1; for(int i = 0; i < x.size(); i++){ ans += x[i] * temp; temp *= 2; } return ans; } }; main(){ Solution ob; vector<int> v = {1,0,1,1,0,1}; ListNode *head = make_list(v); cout << ob.getDecimalValue(head); }
Input
[1,0,1,1,0,1]
Output
45
- Related Articles
- Program to convert Linked list representing binary number to decimal integer in Python
- Program to convert a linked list into a binary search tree in C++
- Program to convert binary search tree to a singly linked list in C++?
- Convert a given Binary Tree to Doubly Linked List (Set 1) in C++
- Convert a given Binary Tree to Doubly Linked List (Set 2) in C++
- Linked List in Binary Tree in C++
- Flatten Binary Tree to Linked List in C++
- Convert singly linked list into circular linked list in C++
- Convert singly linked list into XOR linked list in C++
- Program to convert linked list to zig-zag binary tree in Python
- Python program to convert a given binary tree to doubly linked list
- Program to convert level order binary tree traversal to linked list in Python
- Binary list to integer in Python
- Binary Search on Singly Linked List in C++
- C# program to convert binary string to Integer

Advertisements