Description
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:
- The Linked List is not empty.
- Number of nodes will not exceed
30.
- Each node's value is either
0 or 1.
Solutions
Solution 1: Traverse the Linked List
We use a variable ans to record the current decimal value, with an initial value of 0.
Traverse the linked list. For each node, left-shift ans by one bit, then perform a bitwise OR with the current node's value. After traversal, ans is the decimal value.
The time complexity is O(n), where n is the length of the linked list. The space complexity is O(1).
PythonJavaC++GoTypeScriptRustJavaScriptC#PHPC
# 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:
ans = 0
while head:
ans = ans << 1 | head.val
head = head.next
return ans(code-box)
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode() {}
* ListNode(int val) { this.val = val; }
* ListNode(int val, ListNode next) { this.val = val; this.next = next; }
* }
*/
class Solution {
public int getDecimalValue(ListNode head) {
int ans = 0;
for (; head != null; head = head.next) {
ans = ans << 1 | head.val;
}
return ans;
}
}(code-box)
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode() : val(0), next(nullptr) {}
* ListNode(int x) : val(x), next(nullptr) {}
* ListNode(int x, ListNode *next) : val(x), next(next) {}
* };
*/
class Solution {
public:
int getDecimalValue(ListNode* head) {
int ans = 0;
for (; head; head = head->next) {
ans = ans << 1 | head->val;
}
return ans;
}
};(code-box)
/**
* Definition for singly-linked list.
* type ListNode struct {
* Val int
* Next *ListNode
* }
*/
func getDecimalValue(head *ListNode) (ans int) {
for ; head != nil; head = head.Next {
ans = ans<<1 | head.Val
}
return
}(code-box)
/**
* Definition for singly-linked list.
* class ListNode {
* val: number
* next: ListNode | null
* constructor(val?: number, next?: ListNode | null) {
* this.val = (val===undefined ? 0 : val)
* this.next = (next===undefined ? null : next)
* }
* }
*/
function getDecimalValue(head: ListNode | null): number {
let ans = 0;
for (; head; head = head.next) {
ans = (ans << 1) | head.val;
}
return ans;
}(code-box)
// Definition for singly-linked list.
// #[derive(PartialEq, Eq, Clone, Debug)]
// pub struct ListNode {
// pub val: i32,
// pub next: Option<Box<ListNode>>
// }
//
// impl ListNode {
// #[inline]
// fn new(val: i32) -> Self {
// ListNode {
// next: None,
// val
// }
// }
// }
impl Solution {
pub fn get_decimal_value(mut head: Option<Box<ListNode>>) -> i32 {
let mut ans = 0;
while let Some(node) = head {
ans = (ans << 1) | node.val;
head = node.next;
}
ans
}
}(code-box)
/**
* Definition for singly-linked list.
* function ListNode(val, next) {
* this.val = (val===undefined ? 0 : val)
* this.next = (next===undefined ? null : next)
* }
*/
/**
* @param {ListNode} head
* @return {number}
*/
var getDecimalValue = function (head) {
let ans = 0;
for (; head; head = head.next) {
ans = (ans << 1) | head.val;
}
return ans;
};(code-box)
/**
* Definition for singly-linked list.
* public class ListNode {
* public int val;
* public ListNode next;
* public ListNode(int val=0, ListNode next=null) {
* this.val = val;
* this.next = next;
* }
* }
*/
public class Solution {
public int GetDecimalValue(ListNode head) {
int ans = 0;
for (; head != null; head = head.next) {
ans = ans << 1 | head.val;
}
return ans;
}
}(code-box)
/**
* Definition for a singly-linked list.
* class ListNode {
* public $val = 0;
* public $next = null;
* function __construct($val = 0, $next = null) {
* $this->val = $val;
* $this->next = $next;
* }
* }
*/
class Solution {
/**
* @param ListNode $head
* @return Integer
*/
function getDecimalValue($head) {
$ans = 0;
while ($head !== null) {
$ans = ($ans << 1) | $head->val;
$head = $head->next;
}
return $ans;
}
}(code-box)
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* struct ListNode *next;
* };
*/
int getDecimalValue(struct ListNode* head) {
int ans = 0;
struct ListNode* cur = head;
while (cur) {
ans = (ans << 1) | cur->val;
cur = cur->next;
}
return ans;
}(code-box)