LeetCode 1290 Convert Binary Number in a Linked List to Integer

Crystal Villanueva
2 min readJul 18, 2021

I think this is a great Leetcode problem to get you introduced to linked lists. Linked lists can be so confusing and it sometimes can be overwhelming to understand. This problem will pose as an introduction to understanding linked lists, how to access a node, and continue the linked lists.

“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.”

Steps to solve this problem:

  1. Reiterate the problem
  • “This problem is asking me to go through the linked list, “head”, and it will either be 0 or 1.”

2. Create sample inputs and outputs or just go over the given inputs and outputs to see if you understand.

  • “input: head = [1,0,1], output = 5… I understand that this comes from getting the value from the node and compiling it to be a binary number…What’s a binary number?”
  • When you go over sample inputs and outputs, it can lead you to ask necessary questions to help solve the problem. Now that we know we need to understand what a binary number is and how to achieve it from the values we find, a quick Google search shows that using parseInt with the results in a string and a second argument will produce a binary number: parseInt(“[insert number here]”, 2)

3. Now let’s talk about our approach to solving this problem. To go through a linked list, we can use a while loop and set the head to a variable called current. The linked list while loop will persist if there is a next node, the current node is valid, etc. **if you want to see the full solution, it’s at the bottom.

let current = head
while (current.next !== null) or while (current)

Just know that whatever you put in the argument in the while loop, you have to create a conditional that breaks it so that the while loop doesn’t persist forever.

We will be doing this by assigning what we’re given and assigning it to be the next node, so on and so forth.

current = current.next or current.next = current.next.next

Since we’re in the while loop, lets go ahead and create a variable that takes in all of the values that we encounter. Because parseInt can take in string arguments, I will be initializing the variable to be an empty string.

    let current = head
let temp=""
while (current) {
temp += String(current.val)
current = current.next

}

I incremented my temp string by converting the current value to be a string. Next, I incremented the while loop to be current.next.

Finally, now that we have a temp value of every value that we’ve encountered, lets use parseInt.

return parseInt(temp, 2)

And that’s it!

Here is the full solution:

var getDecimalValue = function(node) {
let current = node
let temp=""
while (current) {
temp += String(current.val)
current = current.next

}
return parseInt(temp, 2)
};

Happy hacking!

-Crystal

--

--