Problem Statement
leetcode problem link
Brute Force
# Definition for singly-linked list.
# class ListNode:
# def __init__(self, val=0, next=None):
# self.val = val
# self.next = next
class Solution:
def getDecimalValue(self, head: Optional[ListNode]) -> int:
curr = head
string = []
while curr:
string.append(str(curr.val))
curr = curr.next
return int(''.join(string), 2)
Editorial
class Solution:
def getDecimalValue(self, head: ListNode) -> int:
num = head.val
while head.next:
num = (num << 1) | head.next.val
head = head.next
return num