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; } };
Preview:
downloadDownload PNG
downloadDownload JPEG
downloadDownload SVG
Tip: You can change the style, width & colours of the snippet with the inspect tool before clicking Download!
Click to optimize width for Twitter