###### tags: `Leetcode` `easy` `list` `bit` `python` `c++`
# 1290. Convert Binary Number in a Linked List to Integer
## [題目連結:] https://leetcode.com/problems/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.
The **most significant bit** is at the head of the linked list.

## 解題想法:
* 題目為求List中,二進位轉十進位
* head.val為最高位元
* 初始最終res=head.val
* while遍歷list
* 每次將res**左移**,並與head.next.val進行**或**運算
* head=head.next
## Python:
``` python=
# Definition for singly-linked list.
# class ListNode(object):
# def __init__(self, val=0, next=None):
# self.val = val
# self.next = next
class Solution(object):
def getDecimalValue(self, head):
"""
:type head: ListNode
:rtype: int
"""
res=head.val
while head.next:
res=(res<<1)|head.next.val
head=head.next
return res
```
## C++:
``` cpp=
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode() : val(0), next(nullptr) {}
* ListNode(int x) : val(x), next(nullptr) {}
* ListNode(int x, ListNode *next) : val(x), next(next) {}
* };
*/
class Solution {
public:
int getDecimalValue(ListNode* head) {
int res=head->val;
while (head->next){
res=(res<<1)|head->next->val;
head=head->next;
}
return res;
}
};
```