Problem statement

https://binarysearch.com/problems/Linked-List-to-Integer/

Solution

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

Complexity

It is O(n) for time and O(1) for space.

Code

class Solution:
    def solve(self, head):
        s = 0
        while head:
            s = 2*s + head.val
            head = head.next
        return s