LC 1290. Convert Binary Number in a Linked List to Integer

Nilanjan Deb · April 5, 2020

This is my CPP solution.

class Solution {
public:
    int getDecimalValue(ListNode* head) {
        vector<int> store;
        ListNode* p = head;
        while(p!=NULL){
            store.push_back(p->val);
            p = p->next;
        }
        int cnt = 0, n = store.size();
        for(int i=0;i<store.size();i++){
            cnt += (pow(2,n-1)*store[i]);
            n--;
        }
        return cnt;
    }
};


Dicussion Forum