Easy
Convert Binary Number in a Linked List to Integer — Python
Full explanation · Time O(n) · Space O(1)
# Time: O(n)
# Space: O(1)
# Definition for singly-linked list.
class ListNode(object):
def __init__(self, x):
self.val = x
self.next = None
class Solution(object):
def getDecimalValue(self, head):
"""
:type head: ListNode
:rtype: int
"""
result = 0
while head:
result = result*2 + head.val
head = head.next
return result