Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 51 additions & 0 deletions Convert_a_Binary_NUmber_in_a_LinkedList_to_interger
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/**
Question

Given head which is a reference node to a singly-linked list. The value of each node in the linked list is either 0 or 1. The linked list holds the binary representation of a number.

Return the decimal value of the number in the linked list.

The most significant bit is at the head of the linked list.

*/






/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode() : val(0), next(nullptr) {}
* ListNode(int x) : val(x), next(nullptr) {}
* ListNode(int x, ListNode *next) : val(x), next(next) {}
* };
*/
class Solution {
public:
int length(ListNode* head ){
int c=0;
ListNode* temp=head;
while(temp!=NULL){
c++;
temp=temp->next;
}
return c;
}

int getDecimalValue(ListNode* head) {
int p=length( head)-1;
ListNode* temp=head;
int sum=0;

while(temp!=NULL){
sum+=(temp->val)*pow(2,p);
p--;
temp=temp->next;
}
return sum;
}
};