Convert binary number in a linked list to integer
October 16, 2022
linked-listProblem URL: Convert binary number in a linked list to integer
We will use a variable res
to store the result. We will iterate over the linked list, and for each node, we will shift res
to the left by 1 bit, and add the value of the current node to res
. Finally, we will return res
.
# 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: ListNode) -> int:
res = 0
while head:
res = 2*res + head.val
head = head.next
return res
Time Complexity: O(n)
Space Complexity: O(1)