Skip to content

Commit 33c5284

Browse files
committed
Resolved 'Convert Binary Number in a Linked List to Integer' LeetCode problem with an optimized solution
1 parent d46bdab commit 33c5284

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# Convert Binary Number in a Linked List to Integer
2+
3+
class ListNode(object):
4+
def __init__(self, val=0, next=None):
5+
self.val = val
6+
self.next = next
7+
8+
class Solution(object):
9+
def getDecimalValue(self, head):
10+
decimal_value = 0
11+
12+
while head:
13+
decimal_value = decimal_value * 2 + head.val
14+
head = head.next
15+
16+
return decimal_value
17+
18+
head = ListNode(1)
19+
head.next = ListNode(0)
20+
head.next.next = ListNode(1)
21+
22+
solution = Solution()
23+
print(solution.getDecimalValue(head))

0 commit comments

Comments
 (0)