Binary Linked List to Integer
Each node holds a 0 or a 1, and the list spells a binary number with the head as the most significant bit. Return its value in decimal. An empty list means 0. The list is passed in as head.
Linked lists appear in the tests as the list of their values, like [1,2,3]. Each node is a ListNode with .val and .next; the class is already defined for you.
Example 1
head [1,0,1]
returns 5
101 in binary is 5.
Example 2
head [0,0,1,1]
returns 3
0011 in binary is 3.
Constraints
0 <= number of nodes <= 30- Each
Node.valis 0 or 1
Write it in your browser, against the clock. The model solution, stepped through line by line, comes after.
More Linked lists problems
- Build a Linked List From an ArrayBeginner
- Build a List of DigitsBeginner
- Build a Range ListBeginner
- Compare Two ListsBeginner
- Count Distinct ValuesBeginner
- Count Even ValuesBeginner
- Count a Target ValueBeginner
- Count the NodesBeginner