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

题目

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode(int x) : val(x), next(NULL) {}
 * };
 */
class Solution {
public:
    int getDecimalValue(ListNode* head) {
        
        
        int ans=0;
        while(head!=NULL)
        {
            ans = (ans<<1)|(head->val);
            head=head->next;
        }
        return ans;
        
    }
};
posted @ 2019-12-16 16:19  Shendu.CC  阅读(177)  评论(0编辑  收藏  举报