# LeetCode 1290. Convert Binary Number in a Linked List to Integer 将链表中二进制转换为整数

Given head which is a reference node to a singly-linked list. The value of each node in the linked list is either 0 or 1. The linked list holds the binary representation of a number.

Return the decimal value of the number in the linked list.

Output: 5
Explanation: (101) in base 2 = (5) in base 10

Output: 0

Output: 1

Output: 18880

Output: 0

• The Linked List is not empty.
• Number of nodes will not exceed 30.
• Each node’s value is either 0 or 1.

1. 链表 -> 数组
2. 整型数组 - > 字符型数组
3. 字符型数组 - > 二进制字符串
4. 二进制字符串 - > 十进制数

# Definition for singly-linked list.
# class ListNode:
#     def __init__(self, x):
#         self.val = x
#         self.next = None

class Solution:
def getDecimalValue(self, head: ListNode) -> int:
arr, str_arr = [], ""
arr = list(map(str, arr))  # list of int -> list of str
str_arr = "".join(arr)  # list of str -> str
return int(str_arr, 2)  # str -> int



Accepted!

Runtime: 24 ms, faster than 88.53% of Python3 online submissions for Convert Binary Number in a Linked List to Integer.

Memory Usage: 12.7 MB, less than 100.00% of Python3 online submissions for Convert Binary Number in a Linked List to Integer.

Time Complexity: O(n)

Space Complexity: O(n)

02-20 61
12-16 173
12-23 171
04-29 89