Akarshan's | Blog

Every Developers Journey is unique. Here's mine.

Leetcode - 1290. Convert Binary Number in a Linked List to Integer

Akarshan MishraAkarshan Mishra

Recursion

Linked List

Leetcode - 1290. Convert Binary Number in a Linked List to Integer
August 6, 2023
A detailed explanation and solution to LeetCode problem 1290: Convert Binary Number in a Linked List to Integer. Learn how to solve this linked list problem using recursion.

The Problem:

In this problem, we are given a linked list that represents a binary number, and we have to convert that into a base10 number (Decimal Number). For example, a linked list 1->0->1 represents 101 which is 5.

Before we begin trying to figure out a solution for this problem, let’s first revisit an important concepts Linked List and Recursion.

Understanding Recursion:

Recursion is essentially a function calling itself. It is frequently used to solve problems that can be divided into smaller, similar sub-problems and can be very useful in dynamic programming. To understand recursion, it can be helpful to break it down into 3 parts:

  1. Base Condition: This condition prevents the recursive function from calling itself infinitely.
  2. Recursive Call: This is responsible for calling the recursive function itself.
  3. Small Calculation: This step, which can come before or after the Recursive Call, is responsible for performing some calculation that we need for the recursive function or for returning some result after a recursive call has been completed.
recursion Image

A recursive tree is a visual representation of the recursive calls made by a function. It can be very helpful in understanding how recursion works and in analyzing the time and space complexity of recursive algorithms.

Understanding Linked List:

A linked list is a linear data structure that consists of a sequence of nodes, each containing data and a reference to the next node in the list. It is often used as an alternative to arrays, as it can be more efficient in certain operations such as insertion and deletion.

To understand linked lists, it can be helpful to break it down into 3 parts:

  1. Nodes: Each node in a linked list contains some data and a reference to the next node in the list.
  2. Head: The head of a linked list is the first node in the list. It is used as the starting point for many operations on the list.
  3. Traversal: Traversing a linked list involves starting at the head and following the references from one node to the next until the desired node is reached or the end of the list is reached.
Linked List

Linked lists can be very useful in solving problems that involve dynamic data structures, as they allow for efficient insertion and deletion of elements. They can also be used in combination with other data structures, such as stacks and queues, to solve more complex problems.

Intuition & Algorithm:

With an understanding of how recursion and linked lists work, let’s try to break down the problem. We don’t need to create our own linked list data structure because it is already provided. This is a fairly straightforward problem, so we will begin by figuring out the base condition, which in this case would be when the recursion reaches the end of the linked list (head == None).

We can pass a string to the recursive function and that string would hold each node's value and when the recursion reaches it's base case, we will return the string. We can then convert the string into integer using the inbuilt method.

This ensures that we will always be able to extract base2 (binary) numbers from a linked list and convert them to base10 (decimal) numbers.

LeetCode - 1290 Main Image

Summary of Solution

Use recursion to traverse the linked list, add the value of each node a string passed as a parameter. Convert the string to base 10 using int() method.

Code

This can be translated to code in the following way:

Solution.py

lang: python

# Definition for singly-linked list.
# class ListNode:
# def __init__(self, val=0, next=None):
# self.val = val
# self.next = next
class Solution:
def getDecimalValue(self, head: ListNode) -> int:
def helper(head, string):
if head == None:
return string
string += str(head.val)
return helper(head.next, string)
binary_string = ""
binary_string = helper( head, binary_string)
return int(binary_string, 2)
  • Time Complexity O(n)
  • Space Complexity O(n)

The time complexity of this solution is O(n) This is because the function needs to traverse the linked lists to calculate the size. Time taken for this operation would be proportional to the size of the linked list.

Space complexity is O(n) because of recursion call stack.

This approach is simple and efficient, making it a good solution to this problem.

I hope you enjoyed this post. Stay tuned for daily LeetCode blog posts!

Latest Posts

Leetcode - 21. Merge Two Sorted Lists
Akarshan MishraAkarshan Mishra

Recursion

Linked List

Leetcode - 21. Merge Two Sorted Lists

August 14, 2023

A detailed explanation and solution to LeetCode problem 21: Merge Two Sorted Lists. Learn how to solve this linked list problem using recursion.

Leetcode - 138. Copy List with Random Pointer
Akarshan MishraAkarshan Mishra

Recursion

Linked List

Leetcode - 138. Copy List with Random Pointer

August 12, 2023

A detailed explanation and solution to LeetCode problem 138: Copy List with Random Pointer. Learn how to solve this linked list problem using recursion.