πΉ 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)
β±οΈ 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)