[LeetCode] 1290. Convert Binary Number in a Linked List to Integer

원숭2·2022년 1월 25일
0

LeetCode

목록 보기
14/51

문제

풀이

  1. LinkedList 안의 모든 수를 nums에 str 형태로 저장함.(∵ join 함수 사용)
  2. int로 변환.

코드

# 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:
        nums = []
        
        while head != None :
            nums.append(str(head.val))
            head = head.next
        
        nums = ''.join(nums)
        
        return int(nums, 2)

0개의 댓글