Easy

Convert Binary Number in a Linked List to IntegerC++

Full explanation · Time O(n) · Space O(1)

// Time:  O(n)
// Space: O(1)

/**
 * 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 result = 0;
        for (; head; head = head->next) {
            result = result * 2 + head->val;
        }
        return result;
    }
};