Skip to content

feat: add solutions to lc problem: No.1290 #4568

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 13, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -76,11 +76,11 @@ tags:

### 方法一:遍历链表

我们用变量 `ans` 记录当前的十进制值,初始值为 $0$。
我们用变量 $\textit{ans}$ 记录当前的十进制值,初始值为 $0$。

遍历链表,对于每个结点,将 `ans` 左移一位,然后再或上当前结点的值。遍历结束后,`ans` 即为十进制值。
遍历链表,对于每个结点,将 $\textit{ans}$ 左移一位,然后再或上当前结点的值。遍历结束后,$\textit{ans}$ 即为十进制值。

时间复杂度 $O(n)$,空间复杂度 $O(1)$。其中 $n$ 为链表的长度
时间复杂度 $O(n)$,其中 $n$ 为链表的长度。空间复杂度 $O(1)$。

<!-- tabs:start -->

Expand Down Expand Up @@ -212,12 +212,11 @@ function getDecimalValue(head: ListNode | null): number {
// }
// }
impl Solution {
pub fn get_decimal_value(head: Option<Box<ListNode>>) -> i32 {
pub fn get_decimal_value(mut head: Option<Box<ListNode>>) -> i32 {
let mut ans = 0;
let mut cur = &head;
while let Some(node) = cur {
while let Some(node) = head {
ans = (ans << 1) | node.val;
cur = &node.next;
head = node.next;
}
ans
}
Expand Down Expand Up @@ -247,6 +246,31 @@ var getDecimalValue = function (head) {
};
```

#### C#

```cs
/**
* 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;
}
}
```

#### PHP

```php
Expand All @@ -267,13 +291,12 @@ class Solution {
* @return Integer
*/
function getDecimalValue($head) {
$rs = [];
while ($head != null) {
array_push($rs, $head->val);
$ans = 0;
while ($head !== null) {
$ans = ($ans << 1) | $head->val;
$head = $head->next;
}
$rsStr = implode($rs);
return bindec($rsStr);
return $ans;
}
}
```
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,13 @@ tags:

<!-- solution:start -->

### Solution 1
### Solution 1: Traverse the Linked List

We use a variable $\textit{ans}$ to record the current decimal value, with an initial value of $0$.

Traverse the linked list. For each node, left-shift $\textit{ans}$ by one bit, then perform a bitwise OR with the current node's value. After traversal, $\textit{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)$.

<!-- tabs:start -->

Expand Down Expand Up @@ -188,12 +194,11 @@ function getDecimalValue(head: ListNode | null): number {
// }
// }
impl Solution {
pub fn get_decimal_value(head: Option<Box<ListNode>>) -> i32 {
pub fn get_decimal_value(mut head: Option<Box<ListNode>>) -> i32 {
let mut ans = 0;
let mut cur = &head;
while let Some(node) = cur {
while let Some(node) = head {
ans = (ans << 1) | node.val;
cur = &node.next;
head = node.next;
}
ans
}
Expand Down Expand Up @@ -223,6 +228,31 @@ var getDecimalValue = function (head) {
};
```

#### C#

```cs
/**
* 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;
}
}
```

#### PHP

```php
Expand All @@ -243,13 +273,12 @@ class Solution {
* @return Integer
*/
function getDecimalValue($head) {
$rs = [];
while ($head != null) {
array_push($rs, $head->val);
$ans = 0;
while ($head !== null) {
$ans = ($ans << 1) | $head->val;
$head = $head->next;
}
$rsStr = implode($rs);
return bindec($rsStr);
return $ans;
}
}
```
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/**
* 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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,11 @@ class Solution {
* @return Integer
*/
function getDecimalValue($head) {
$rs = [];
while ($head != null) {
array_push($rs, $head->val);
$ans = 0;
while ($head !== null) {
$ans = ($ans << 1) | $head->val;
$head = $head->next;
}
$rsStr = implode($rs);
return bindec($rsStr);
return $ans;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,11 @@
// }
// }
impl Solution {
pub fn get_decimal_value(head: Option<Box<ListNode>>) -> i32 {
pub fn get_decimal_value(mut head: Option<Box<ListNode>>) -> i32 {
let mut ans = 0;
let mut cur = &head;
while let Some(node) = cur {
while let Some(node) = head {
ans = (ans << 1) | node.val;
cur = &node.next;
head = node.next;
}
ans
}
Expand Down