20.4.24 二进制链表转整数 简单 1290

解题思路

  1. 代码很短能看懂,主要是第一次接触到位运算,左移乘2,右移除2.

代码

/**
 * 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 sum=0;
        ListNode *curr=head;
        while(curr){
            sum=sum*2+curr->val;
            curr=curr->next;
        }
        return sum;
    }
};
posted @ 2020-04-24 15:20  肥斯大只仔  阅读(101)  评论(0编辑  收藏  举报