Skip to content

Commit 3bb96c2

Browse files
authored
Create convert-binary-number-in-a-linked-list-to-integer.py
1 parent d8de32f commit 3bb96c2

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# Time: O(n)
2+
# Space: O(1)
3+
4+
# Definition for singly-linked list.
5+
class ListNode(object):
6+
def __init__(self, x):
7+
self.val = x
8+
self.next = None
9+
10+
11+
class Solution(object):
12+
def getDecimalValue(self, head):
13+
"""
14+
:type head: ListNode
15+
:rtype: int
16+
"""
17+
result = 0
18+
while head:
19+
result = result*2 + head.val
20+
head = head.next
21+
return result

0 commit comments

Comments
 (0)