1290. 二进制链表转整数 Convert Binary Number in a Linked List to Integer

题目 https://leetcode-cn.com/problems/convert-binary-number-in-a-linked-list-to-integer/

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     struct ListNode *next;
 * };
 */


int getDecimalValue(struct ListNode* head){
    int num = 0;
    struct ListNode* node = head;
    while(node != NULL){
        num = num<<1;
        num |= node->val;
        node = node->next;
    }
    return num;
}

 

你可能感兴趣的:(C,LeetCode)