1. What I learned
a. class LinkedList
‘self.head’ in ‘def init(self)’ of class ‘LinkedList’ refers to the head node.
b. int(num1,num2) method
It converts ‘num1’ in given based(‘num2’).
ex) int(101,2) –> 5
2. How I sloved
I sent a linked list to Class ‘solution’. I add the node’s value to ‘num’ and moved to the next node. I kept doing this for all the nodes, and I returned the result.
3. Code
class Solution:
def getDecimalValue(self, head: ListNode) -> int:
num = 0
while head:
num = 2*num+head.val
head = head.next
return num
class Solution:
def getDecimalValue(self, head: ListNode) -> int:
num = ''
while head:
num += str(head.val)
head = head.next
return int(num,2)
4. Result
Runtime : 20 ms(97.70%), Memory usage : 13.9 MB(100.00%)
(Runtime can be different by a system even if it is a same code.)