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.
C++
#include <bits/stdc++.h>
using namespace std;
class Node
{
public :
bool data;
Node* next;
};
int decimalValue(Node *head)
{
int res = 0;
while (head != NULL)
{
res = (res << 1) + head->data;
head = head->next;
}
return res;
}
Node *newNode( bool data)
{
Node *temp = new Node;
temp->data = data;
temp->next = NULL;
return temp;
}
int main()
{
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;
}
|
C
#include<iostream>
using namespace std;
struct Node
{
bool data;
struct Node* next;
};
int decimalValue( struct Node *head)
{
int res = 0;
while (head != NULL)
{
res = (res << 1) + head->data;
head = head->next;
}
return res;
}
Node *newNode( bool data)
{
struct Node *temp = new Node;
temp->data = data;
temp->next = NULL;
return temp;
}
int main()
{
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
class GFG
{
static class Node
{
boolean data;
Node next;
};
static int decimalValue( Node head)
{
int res = 0 ;
while (head != null )
{
res = (res << 1 ) + (head.data? 1 : 0 );
head = head.next;
}
return res;
}
static Node newNode( int data)
{
Node temp = new Node();
temp.data = (data== 1 ? true : false );
temp.next = null ;
return temp;
}
public static void main(String args[])
{
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));
}
}
|
Python3
class Node:
def __init__( self , data):
self .data = data
self . next = None
class LinkedList:
def __init__( self ):
self .head = None
def decimalValue( self , head):
res = 0
while head:
res = (res << 1 ) + head.data
head = head. next
return res
if __name__ = = '__main__' :
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)))
|
C#
using System;
class GFG
{
public class Node
{
public Boolean data;
public Node next;
};
static int decimalValue( Node head)
{
int res = 0;
while (head != null )
{
res = (res << 1) + (head.data ? 1 : 0);
head = head.next;
}
return res;
}
static Node newNode( int data)
{
Node temp = new Node();
temp.data = (data == 1 ? true : false );
temp.next = null ;
return temp;
}
public static void Main(String []args)
{
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));
}
}
|
Javascript
<script>
class Node {
constructor(){
this .data = true ;
this .next = null ;
}
}
function decimalValue(head) {
var res = 0;
while (head != null ) {
res = (res << 1) + (head.data ? 1 : 0);
head = head.next;
}
return res;
}
function newNode(data) {
var temp = new Node();
temp.data = (data == 1 ? true : false );
temp.next = null ;
return temp;
}
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));
</script>
|
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.
Another Approach(by reversing the Linked List):
Follow the below steps to solve the given problem
1) First reverse the given linked list.
2) Initialize a ans variable to store ans and pos variable to keep track of position of node in linked list.
3) Perform the operation ans = ans + (rhead.data*(2**pos))%MOD)
4) perform ans = ans%MOD
Below is the implementation of above approach:
C++
#include<bits/stdc++.h>
using namespace std;
struct Node{
int data;
Node* next;
Node( int data){
this ->data = data;
this ->next = NULL;
}
};
long long unsigned int power( int num, int count){
if (count ==0) return 1;
if (count%2==0)
return (power(num,count/2)%1000000007)*(power(num,count/2)%1000000007);
else
return num*(power(num,count/2)%1000000007)*(power(num,count/2)%1000000007);;
}
Node* reverse(Node* head){
if (head == NULL || head->next == NULL) return head;
Node* curr = head;
Node* prev = NULL;
Node* nxt = head->next;
while (nxt != NULL){
curr->next = prev;
prev = curr;
curr = nxt;
nxt = nxt->next;
}
curr->next = prev;
return curr;
}
int decimalValue(Node* head){
int MOD = 1000000007;
Node* rhead = reverse(head);
int ans = 0;
int pos = 0;
while (rhead != NULL){
ans = (ans%MOD + ((rhead->data)*power(2,pos)) % MOD) % MOD;
rhead = rhead->next;
pos++;
}
return ans;
}
int main(){
Node* head = new Node(1);
head->next = new Node(0);
head->next->next = new Node(1);
head->next->next->next = new Node(1);
cout<< "Decimal Value is : " <<decimalValue(head);
}
|
Java
import java.util.*;
class Node{
public int data;
public Node next;
public Node( int item){
data = item;
next = null ;
}
}
public class LinkedList{
static int power( int num, int count){
if (count == 0 ) return 1 ;
if (count % 2 == 0 )
return (power(num,count/ 2 )% 1000000007 )*(power(num,count/ 2 )% 1000000007 );
else
return num*(power(num,count/ 2 )% 1000000007 )*(power(num,count/ 2 )% 1000000007 );
}
static Node reverse(Node head){
if (head == null || head.next == null ) return head;
Node curr = head;
Node prev = null ;
Node nxt = head.next;
while (nxt != null ){
curr.next = prev;
prev = curr;
curr = nxt;
nxt = nxt.next;
}
curr.next = prev;
return curr;
}
static int decimalValue(Node head){
Node rhead = reverse(head);
int ans = 0 ;
int pos = 0 ;
while (rhead != null ){
ans = (ans % 1000000007 + ((rhead.data)*(( int )power( 2 ,pos))) % 1000000007 ) % 1000000007 ;
rhead = rhead.next;
pos++;
}
return ans;
}
public static void main(String args[]){
Node head = new Node( 1 );
head.next = new Node( 0 );
head.next.next = new Node( 1 );
head.next.next.next = new Node( 1 );
System.out.println( "Decimal Value is : " + decimalValue(head));
}
}
|
Python3
class Node:
def __init__( self , data):
self .data = data
self . next = None
def reverse(head):
if (head = = None or head. next = = None ):
return head
curr = head
prev = None
nxt = head. next
while (nxt):
curr. next = prev
prev = curr
curr = nxt
nxt = nxt. next
curr. next = prev
return curr
def decimalValue(head):
MOD = 10 * * 9 + 7
rhead = reverse(head)
ans = 0
pos = 0
while rhead:
ans = (ans % MOD + (rhead.data * ( 2 * * pos)) % MOD) % MOD
rhead = rhead. next
pos + = 1
return ans
head = Node( 1 )
head. next = Node( 0 )
head. next . next = Node( 1 )
head. next . next . next = Node( 1 )
print ( "Decimal Value is :" , end = " " )
print (decimalValue(head))
|
C#
using System;
class GFG{
public class Node{
public int data;
public Node next;
public Node( int data){
this .data = data;
this .next = null ;
}
}
static int power( int num, int count){
if (count == 0) return 1;
if (count%2==0)
return (power(num,count/2)%1000000007)*(power(num,count/2)%1000000007);
else
return num*(power(num,count/2)%1000000007)*(power(num,count/2)%1000000007);
}
static Node reverse(Node head){
if (head == null || head.next == null ) return head;
Node curr = head;
Node prev = null ;
Node nxt = head.next;
while (nxt != null ){
curr.next = prev;
prev = curr;
curr = nxt;
nxt = nxt.next;
}
curr.next = prev;
return curr;
}
static int decimalValue(Node head){
int MOD = 1000000007;
Node rhead = reverse(head);
int ans = 0;
int pos = 0;
while (rhead != null ){
ans = (ans%MOD + ((rhead.data)*power(2,pos)) % MOD) % MOD;
rhead = rhead.next;
pos++;
}
return ans;
}
public static void Main(){
Node head = new Node(1);
head.next = new Node(0);
head.next.next = new Node(1);
head.next.next.next = new Node(1);
Console.WriteLine( "Decimal Value is : " + decimalValue(head));
}
}
|
Javascript
class Node{
constructor(data){
this .data = data;
this .left = null ;
this .right = null ;
}
}
function reverse(head){
if (head == null || head.next == null ) return head;
let curr = head;
let prev = null ;
let nxt = head.next;
while (nxt != null ){
curr.next = prev;
prev = curr;
curr = nxt;
nxt = nxt.next;
}
curr.next = prev;
return curr;
}
function decimalValue(head){
let MOD = 1000000007;
let rhead = reverse(head);
let ans = 0;
let pos = 0;
while (rhead != null ){
ans = (ans % MOD+(rhead.data*(2**pos)) % MOD) % MOD;
rhead = rhead.next;
pos += 1;
}
return ans;
}
let head = new Node(1);
head.next = new Node(0);
head.next.next = new Node(1);
head.next.next.next= new Node(1);
console.log( "Decimal Value is : " + decimalValue(head));
|
Output
Decimal Value is : 11
Time Complexity: O(N) where N is the number of nodes in linked list.
Auxiliary Space: O(1)
Last Updated :
28 Dec, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...