Leetcode 分享筆記

1290. Convert Binary Number in a Linked List to Integer

題目

leetcode
b

python


class Solution(object):
    def getDecimalValue(self, head):
        """
        :type head: ListNode
        :rtype: int
        """
        curr = head
        ans = []
        output = 0
        while curr != None:
            ans.append(curr.val)
            curr = curr.next
        for i in range(len(ans)):
            if ans[i] == 1:
                output += 2**(len(ans)-1-i)
            else:
                output += 0
        return output