DEV Community

Md. Rishat Talukder
Md. Rishat Talukder

Posted on

🧠 Solving LeetCode Until I Become Top 1% β€” Day `43`

πŸ”Ή Problem: 1290. Convert Binary Number in a Linked List to Integer

Difficulty: #Easy
Tags: #LinkedList, #Math, #BitManipulation


πŸ“ Problem Summary

You’re given the head of a singly linked list where each node contains either a 0 or a 1, and the entire linked list represents a binary number (most significant bit comes first).

Your task is to convert that binary number to its decimal (base 10) form and return it as an integer.


🧠 My Thought Process

  • Brute Force Idea:

    • Traverse the linked list and append each bit to a string.
    • After the traversal, use Python's built-in int(binary_string, 2) to convert it.
  • Optimized Strategy:

    • Although string-building is simple, we could optimize space by keeping a running integer.
    • For every bit b, left-shift the current result (res = res * 2 + b).

But for now, the string approach is fast and clear, especially for small constraints.

  • Algorithm Used: Basic linked list traversal and binary string conversion.

βš™οΈ Code Implementation (Python)

class Solution:
    def getDecimalValue(self, head: Optional[ListNode]) -> int:
        s = ''
        while head:
            s += str(head.val)
            head = head.next
        return int(s, 2)
Enter fullscreen mode Exit fullscreen mode

⏱️ Time & Space Complexity

  • Time: O(n) β€” one traversal through the list
  • Space: O(n) β€” string s stores n bits

⚠️ This could be optimized to O(1) space by using integer shifting instead of a string.


🧩 Key Takeaways

  • βœ… I practiced converting binary to decimal using both string manipulation and bit math.
  • πŸ’‘ It's okay to start with readable solutions first, then optimize if necessary.
  • πŸ’­ Bit manipulation and linked list problems often come together β€” be prepared for that combo.

πŸ” Reflection (Self-Check)

  • [x] Could I solve this without help?
  • [x] Did I write code from scratch?
  • [x] Did I understand why it works?
  • [x] Will I be able to recall this in a week?

πŸš€ Progress Tracker

Metric Value
Day 43
Total Problems Solved 384
Confidence Today πŸ˜ƒ
Leetcode Rating 1572

Top comments (0)