Difficulty
Easy
Language
Python
Solution 1 - Build Number While Traversing
Shift left and add current bit.
Shift left and add current bit.
""" 1290.1 - Convert Binary Number in a Linked List to Integer - Solution 1 - Traverse """
from typing import Optional
class ListNode:
def __init__(self, val=0, next=None):
self.val = val
self.next = next
class Solution:
def getDecimalValue(self, head: Optional[ListNode]) -> int:
res = 0
cur = head
while cur:
res = (res << 1) | cur.val
cur = cur.next
return res