Convert Binary Number in a Linked List to Integer LeetCode solution in Python

1
# Definition for singly-linked list.
2
# class ListNode:
3
# def __init__(self, val=0, next=None):
4
# self.val = val
5
# self.next = next
6
class Solution:
7
def getDecimalValue(self, head: ListNode) -> int:
8
res = 0
9
po = 0
10
stack = []
11
node = head
12
while node:
13
stack.append(node.val)
14
node = node.next
15
res = 0
16
for i in reversed(stack):
17
res += i * (2**po)
18
po += 1
19
return res

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0