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

`Input: head = [1,0,1]Output: 5Explanation: (101) in base 2 = (5) in base 10`
`class ListNode:    def __init__(self, x):        self.val = x        self.next = Noneclass LinkList:    def __init__(self):        self.head=Nonedef initList(self, data):        # Creat head node        self.head = ListNode(data[0])        r=self.head        p = self.head        # Create node for every data        for i in data[1:]:            node = ListNode(i)            p.next = node            p = p.next        return r    def printlist(self,head):        if head == None: return        node = head        while node != None:            #print(node.val,end=' ')            node = node.next`
1. Convert n to list-node.
2. Calculate binary to decimal based on location.
`class Solution:    def getDecimalValue(self, head):        t, node_n=head, 0        while(t):            node_n+=1            t=t.next        sum_n=0        for i in range(node_n):            x=head.val            sum_n+=x*2**(node_n-1-i)            head=head.next        return sum_n`

--

--

## More from Fatboy Slim

Interesting in any computer science.

Love podcasts or audiobooks? Learn on the go with our new app.

## Fatboy Slim

101 Followers

Interesting in any computer science.