Convert Binary Number in a Linked List to Integer

PHOTO EMBED

Thu Jun 16 2022 12:17:22 GMT+0000 (Coordinated Universal Time)

Saved by @shivam_2009 #c++

class Solution {
public:
    int getDecimalValue(ListNode* head) {
      ListNode* temp=head;
        int sum=0;
        int count =0;
        while(temp!=NULL)
        {
          count++;
          temp=temp->next;
        }
         ListNode* temp2=head;
        while(temp2!=NULL)
        {
            if(temp2->val==1)
            {
              sum+=(pow(2,count-1));
            }
            temp2=temp2->next;
            count--;
        }   
          return sum;
    }
};
content_copyCOPY