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.
The most significant bit is at the head of the linked list.
Example 1:
Input: head = [1,0,1] Output: 5 Explanation: (101) in base 2 = (5) in base 10
Example 2:
Input: head = [0] Output: 0
Constraints:
30
.0
or 1
.When you get asked this question in a real-life environment, it will often be ambiguous (especially at FAANG). Make sure to ask these questions in that case:
We're given a linked list representing a binary number and we want to find its decimal equivalent. The brute force approach involves figuring out the complete binary number first, then converting that entire number into its decimal form at once.
Here's how the algorithm would work step-by-step:
class ListNode:
def __init__(self, val=0, next=None):
self.val = val
self.next = next
def getDecimalValue(head):
binary_string = ""
current_node = head
# Traverse the linked list to construct the binary string
while current_node:
binary_string += str(current_node.val)
current_node = current_node.next
# Convert the binary string to an integer
decimal_value = 0
power = 0
# Iterate through the binary string in reverse.
for bit in reversed(binary_string):
if bit == '1':
decimal_value += 2 ** power
power += 1
return decimal_value
We need to turn a linked list representing a binary number into its integer equivalent. The most efficient way to do this is to construct the integer representation bit by bit as we traverse the list, leveraging the binary number system's properties.
Here's how the algorithm would work step-by-step:
class ListNode:
def __init__(self, val=0, next=None):
self.val = val
self.next = next
def get_decimal_value(head: ListNode) -> int:
decimal_value = 0
current_node = head
# Iterate through the linked list
while current_node:
# Shift existing bits to the left, making space for the new bit.
decimal_value = decimal_value * 2
# Add the current node's value (0 or 1) to the result.
decimal_value = decimal_value + current_node.val
current_node = current_node.next
return decimal_value
Case | How to Handle |
---|---|
Empty list | Return 0 immediately as an empty list represents the integer 0. |
Single node list with value 0 | Return 0 as the single node list [0] represents the integer 0. |
Single node list with value 1 | Return 1 as the single node list [1] represents the integer 1. |
List with all zeros | This represents the integer 0, and the iterative or recursive solution should handle it correctly by resulting in 0. |
Very long list approaching integer overflow | Use a long data type or consider using BigInteger in languages like Java or Python to prevent integer overflow, or check for overflow during calculation. |
List contains values other than 0 or 1 | Throw an IllegalArgumentException or return an error code, as the input is invalid for a binary number representation. |
List representing the maximum possible integer value for the system architecture | The conversion process should correctly calculate and return the maximum integer value without overflow if `long` data type is used. |
List starting with multiple leading zeros | The conversion algorithm should correctly interpret these leading zeros without affecting the resulting integer value (e.g., [0,0,1] should return 1). |