解法一:
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* struct ListNode *next;
* };
*/
int getDecimalValue(struct ListNode* head){
int number=0;
struct ListNode *p=head;
while(p!=NULL)
{
number=number*2+p->val;
p=p->next;
}
return number;
}
解法二:使用位运算,简洁
int getDecimalValue(struct ListNode* head){
int res = 0;
for ( ; head; head = head->next)
res = (res << 1) | head->val;
return res;
}