what went wrong.. please help.. not working for [1,0,0,0,1,0,0,1,1,1,1,1,1,1,0,1,1,0,0,1,1]
class Solution 
{
    public:     //program to Convert Binary Number in a Linked List to Integer
        int getDecimalValue(ListNode* head) 
        {
            if(head== NULL)
                return 0;
            if(head->next == NULL)
            {
                if(head->val == 0)
                    return 0;
                else return 1;
            }
            ListNode * temp = head;
            unsigned long num = 0;
            while(temp)
            {
                num *= 10;
                num += temp->val;
                temp = temp->next;
            }
            unsigned long base = 1;
            unsigned long dec = 0;
            unsigned long Last_digit = 0;
            while(num)
            {
                Last_digit = num%10; num /= 10;
                dec += Last_digit*base;
                base *= 2;
            }
            return
                dec;
        }
};
Comments (0)