C++
Java
Python
Python3
C
C#
JavaScript
Ruby
Swift
Go
Scala
Kotlin
Rust
PHP
TypeScript
Racket
Erlang
Elixir
Dart
monokai
ambiance
chaos
chrome
cloud9_day
cloud9_night
cloud9_night_low_color
clouds
clouds_midnight
cobalt
crimson_editor
dawn
dracula
dreamweaver
eclipse
github
github_dark
gob
gruvbox
gruvbox_dark_hard
gruvbox_light_hard
idle_fingers
iplastic
katzenmilch
kr_theme
kuroir
merbivore
merbivore_soft
mono_industrial
nord_dark
one_dark
pastel_on_dark
solarized_dark
solarized_light
sqlserver
terminal
textmate
tomorrow
tomorrow_night
tomorrow_night_blue
tomorrow_night_bright
tomorrow_night_eighties
twilight
vibrant_ink
xcode
上次编辑到这里,代码来自缓存 点击恢复默认模板
/**
* 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) {
}
};
运行代码
提交
rust 解法, 执行用时: 0 ms, 内存消耗: 1.9 MB, 提交时间: 2023-09-15 15:14:31
// 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(head: Option<Box<ListNode>>) -> i32 {
let mut current = head;
let mut ret = 0;
while let Some(mut tmp) = current.take() {
let next = tmp.next.take();
ret = ret * 2 + tmp.val;
current = next;
}
ret
}
pub fn get_decimal_value2(head: Option<Box<ListNode>>) -> i32 {
let mut ans = 0;
let mut cur = head;
while let Some(node) = cur {
ans = (ans << 1) | node.val;
cur = node.next;
}
ans
}
}
cpp 解法, 执行用时: 4 ms, 内存消耗: 8.4 MB, 提交时间: 2023-09-15 15:12:40
/**
* 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 res = 0;
while ( head ) {
res <<= 1;
res += head->val;
head = head->next;
}
return res;
}
};
java 解法, 执行用时: 0 ms, 内存消耗: 38.9 MB, 提交时间: 2023-09-15 15:11:40
/**
* 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 res = 0;
while ( head != null ) {
res <<= 1;
res += head.val;
head = head.next;
}
return res;
}
}
python3 解法, 执行用时: 32 ms, 内存消耗: 15.8 MB, 提交时间: 2023-09-15 15:11:02
# 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:
res = 0
while head != None:
res <<= 1
res += head.val
head = head.next
return res
golang 解法, 执行用时: 0 ms, 内存消耗: 2 MB, 提交时间: 2021-05-28 13:55:48
/**
* Definition for singly-linked list.
* type ListNode struct {
* Val int
* Next *ListNode
* }
*/
func getDecimalValue(head *ListNode) int {
res := 0
for head != nil {
res <<= 1
res += head.Val
head = head.Next
}
return res
}
php 解法, 执行用时: 4 ms, 内存消耗: 15.1 MB, 提交时间: 2021-05-14 18:28:00
/**
* Definition for a singly-linked list.
* class ListNode {
* public $val = 0;
* public $next = null;
* function __construct($val) { $this->val = $val; }
* }
*/
class Solution {
/**
* @param ListNode $head
* @return Integer
*/
function getDecimalValue($head) {
if ( $head == null ) return 0;
$ans = 0;
while ( $head != null ) {
$ans = ( $ans << 1) + $head->val;
$head = $head->next;
}
return $ans;
}
}