---
title: 'LeetCode 1290. Convert Binary Number in a Linked List to Integer'
disqus: hackmd
---
# LeetCode 1290. Convert Binary Number in a Linked List to Integer
## Description
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.
## Example
Input: head = [1,0,1]
Output: 5
Explanation: (101) in base 2 = (5) in base 10
## Constraints
The Linked List is not empty.
Number of nodes will not exceed 30.
Each node's value is either 0 or 1.
## Answer
此題可將list中的val一個個抓出來疊加,並將舊值<<1上去,即可得答案。
```Cin=
//2021_11_29
int getDecimalValue(struct ListNode* head) {
int ans = 0;
while(head){
ans = (ans << 1) | head->val;
head = head->next;
}
return ans;
}
```
## Link
https://leetcode.com/problems/convert-binary-number-in-a-linked-list-to-integer/
###### tags: `Leetcode`