1290. Convert Binary Number in a Linked List to Integer
Problem
Tags: Linked List
, Math
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.
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
or1
.
Code
C
// 1290. Convert Binary Number in a Linked List to Integer (8/8/53904)
// Runtime: 3 ms (33.49%) Memory: 5.59 MB (84.17%)
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* struct ListNode *next;
* };
*/
int getDecimalValue(struct ListNode* head) {
long long result = 0;
while(head) {
result <<= 1;
result += head->val;
head = head->next;
}
return result;
}
CPP
// 1290. Convert Binary Number in a Linked List to Integer (8/9/53904)
// Runtime: 0 ms (92.13%) Memory: 8.30 MB (44.87%)
/**
* 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) {
long long result = 0;
while(head) {
result <<= 1;
result += head->val;
head = head->next;
}
return result;
}
};
GO
// 1290. Convert Binary Number in a Linked List to Integer (8/14/53904)
// Runtime: 0 ms (93.38%) Memory: 2.05 MB (0.00%)
/**
* Definition for singly-linked list.
* type ListNode struct {
* Val int
* Next *ListNode
* }
*/
func getDecimalValue(head *ListNode) int {
result := 0
for head != nil {
result <<= 1
result += head.Val
head = head.Next
}
return result
}
JAVA
// 1290. Convert Binary Number in a Linked List to Integer (8/16/53904)
// Runtime: 0 ms (93.94%) Memory: 36.30 MB (93.00%)
/**
* 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) {
var result = 0;
while (head != null) {
result <<= 1;
result += head.val;
head = head.next;
}
return result;
}
}
JS
// 1290. Convert Binary Number in a Linked List to Integer (8/6/53904)
// Runtime: 91 ms (23.28%) Memory: 38.52 MB (94.73%)
/**
* 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}
*/
function getDecimalValue(head) {
let result = 0;
while(head) {
result <<= 1;
result += head.val;
head = head.next;
}
return result;
};
PHP
// 1290. Convert Binary Number in a Linked List to Integer (8/17/53904)
// Runtime: 8 ms (72.22%) Memory: 15.56 MB (83.33%)
/**
* 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) {
$result = 0;
while ($head != null) {
$result <<= 1;
$result += $head->val;
$head = $head->next;
}
return $result;
}
}
PY
# 1290. Convert Binary Number in a Linked List to Integer (8/15/53904)
# Runtime: 16 ms (81.39%) Memory: 13.33 MB (52.02%)
# Definition for singly-linked list.
# class ListNode(object):
# def __init__(self, val=0, next=None):
# self.val = val
# self.next = next
class Solution(object):
def getDecimalValue(self, head):
"""
:type head: ListNode
:rtype: int
"""
result = 0
while head != None:
result <<= 1
result += head.val
head = head.next
return result
PY3
# 1290. Convert Binary Number in a Linked List to Integer (8/15/53904)
# Runtime: 20 ms (94.63%) Memory: 14.18 MB (0.00%)
# 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:
result = 0
while head != None:
result <<= 1
result += head.val
head = head.next
return result
TS
// 1290. Convert Binary Number in a Linked List to Integer (8/10/53904)
// Runtime: 80 ms (53.97%) Memory: 40.79 MB (90.48%)
/**
* 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 result = 0;
while(head !== null) {
result <<= 1;
result += head.val;
head = head.next;
}
return result;
};