#1290
Easy Algorithms Convert binary number in a linked list to integer
Linked List Math
82.3% acceptance
Feb 25, 2026
4668
177
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.
Solution
Rust
Time O(n)
Space O(1)
impl Solution {
pub fn get_decimal_value(mut head: Option<Box<ListNode>>) -> i32 {
let mut result = 0;
while let Some(node) = head {
result = result * 2 + node.val;
head = node.next;
}
result
}
}