leetcode 1290.二进制链表转整数

⭐️ 题目描述

leetcode 1290.二进制链表转整数_第1张图片


leetcode链接:二进制链表转整数

ps:

 1 0 1
(0 << 1) + 1 = 0 + 1 = 1
(1 << 1) + 0 = 2 + 0 = 2
(2 << 1) + 1 = 4 + 1 = 5

代码:

int getDecimalValue(struct ListNode* head)
{
    int ans = 0;
    while(head != NULL)
    {
        ans = (ans << 1) + head->val; //左移1位,相当于乘以2

        printf("%d " , ans);
        head = head->next;
    }
    return ans;
}

你可能感兴趣的:(刷题,leetcode,链表,学习)