--- title : Convert Binary Number in a Linked List to Integer tags : --- ###### tags: `LeetCode`, Convert Binary Number in a Linked List to Integer === ## Problem `1290` 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. Source --- https://leetcode.com/problems/convert-binary-number-in-a-linked-list-to-integer/ Problem Analytica --- This problem is to convert a linkedlist binary number i.e. $(101)_2$ into an integer $1\cdot 2^2 + 0 \cdot 2^1 + 1 \cdot 2^0 = 5$ Point of Attack --- :::spoiler Spoiler Alert Since the list is starting from higher order digit `1->0->1` , we can simply apply arithmetic $(((1)\cdot2+0)\cdot2)+1) \equiv 1\cdot 2^2 + 0 \cdot 2^1 + 1 \cdot 2^0$ Borrowing the very explainning graph from leet code solution ![](https://leetcode.com/problems/convert-binary-number-in-a-linked-list-to-integer/Figures/1290/try1.png) The solution is quite straight forward ::: Algorithm --- :::spoiler Spoiler Alert ````cpp int getDecimalValue(ListNode* head){ int value = 0; while(head != nullptr){ value = (value << 1) | head->val; head = head->next; } return value; } ```` ::: Hierarcy Decompsition --- Further Readings --- ###### tags: `LeetCode`, `Bit Manipulation`