Skip to content

Commit 35bc2f9

Browse files
committed
feat: add solutions to lc problem: No.1290
No.1290.Convert Binary Number in a Linked List to Integer
1 parent 5c30065 commit 35bc2f9

File tree

5 files changed

+101
-31
lines changed

5 files changed

+101
-31
lines changed

solution/1200-1299/1290.Convert Binary Number in a Linked List to Integer/README.md

Lines changed: 35 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -76,11 +76,11 @@ tags:
7676

7777
### 方法一:遍历链表
7878

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

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

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

8585
<!-- tabs:start -->
8686

@@ -212,12 +212,11 @@ function getDecimalValue(head: ListNode | null): number {
212212
// }
213213
// }
214214
impl Solution {
215-
pub fn get_decimal_value(head: Option<Box<ListNode>>) -> i32 {
215+
pub fn get_decimal_value(mut head: Option<Box<ListNode>>) -> i32 {
216216
let mut ans = 0;
217-
let mut cur = &head;
218-
while let Some(node) = cur {
217+
while let Some(node) = head {
219218
ans = (ans << 1) | node.val;
220-
cur = &node.next;
219+
head = node.next;
221220
}
222221
ans
223222
}
@@ -247,6 +246,31 @@ var getDecimalValue = function (head) {
247246
};
248247
```
249248

249+
#### C#
250+
251+
```cs
252+
/**
253+
* Definition for singly-linked list.
254+
* public class ListNode {
255+
* public int val;
256+
* public ListNode next;
257+
* public ListNode(int val=0, ListNode next=null) {
258+
* this.val = val;
259+
* this.next = next;
260+
* }
261+
* }
262+
*/
263+
public class Solution {
264+
public int GetDecimalValue(ListNode head) {
265+
int ans = 0;
266+
for (; head != null; head = head.next) {
267+
ans = ans << 1 | head.val;
268+
}
269+
return ans;
270+
}
271+
}
272+
```
273+
250274
#### PHP
251275

252276
```php
@@ -267,13 +291,12 @@ class Solution {
267291
* @return Integer
268292
*/
269293
function getDecimalValue($head) {
270-
$rs = [];
271-
while ($head != null) {
272-
array_push($rs, $head->val);
294+
$ans = 0;
295+
while ($head !== null) {
296+
$ans = ($ans << 1) | $head->val;
273297
$head = $head->next;
274298
}
275-
$rsStr = implode($rs);
276-
return bindec($rsStr);
299+
return $ans;
277300
}
278301
}
279302
```

solution/1200-1299/1290.Convert Binary Number in a Linked List to Integer/README_EN.md

Lines changed: 39 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,13 @@ tags:
5656

5757
<!-- solution:start -->
5858

59-
### Solution 1
59+
### Solution 1: Traverse the Linked List
60+
61+
We use a variable $\textit{ans}$ to record the current decimal value, with an initial value of $0$.
62+
63+
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.
64+
65+
The time complexity is $O(n)$, where $n$ is the length of the linked list. The space complexity is $O(1)$.
6066

6167
<!-- tabs:start -->
6268

@@ -188,12 +194,11 @@ function getDecimalValue(head: ListNode | null): number {
188194
// }
189195
// }
190196
impl Solution {
191-
pub fn get_decimal_value(head: Option<Box<ListNode>>) -> i32 {
197+
pub fn get_decimal_value(mut head: Option<Box<ListNode>>) -> i32 {
192198
let mut ans = 0;
193-
let mut cur = &head;
194-
while let Some(node) = cur {
199+
while let Some(node) = head {
195200
ans = (ans << 1) | node.val;
196-
cur = &node.next;
201+
head = node.next;
197202
}
198203
ans
199204
}
@@ -223,6 +228,31 @@ var getDecimalValue = function (head) {
223228
};
224229
```
225230

231+
#### C#
232+
233+
```cs
234+
/**
235+
* Definition for singly-linked list.
236+
* public class ListNode {
237+
* public int val;
238+
* public ListNode next;
239+
* public ListNode(int val=0, ListNode next=null) {
240+
* this.val = val;
241+
* this.next = next;
242+
* }
243+
* }
244+
*/
245+
public class Solution {
246+
public int GetDecimalValue(ListNode head) {
247+
int ans = 0;
248+
for (; head != null; head = head.next) {
249+
ans = ans << 1 | head.val;
250+
}
251+
return ans;
252+
}
253+
}
254+
```
255+
226256
#### PHP
227257

228258
```php
@@ -243,13 +273,12 @@ class Solution {
243273
* @return Integer
244274
*/
245275
function getDecimalValue($head) {
246-
$rs = [];
247-
while ($head != null) {
248-
array_push($rs, $head->val);
276+
$ans = 0;
277+
while ($head !== null) {
278+
$ans = ($ans << 1) | $head->val;
249279
$head = $head->next;
250280
}
251-
$rsStr = implode($rs);
252-
return bindec($rsStr);
281+
return $ans;
253282
}
254283
}
255284
```
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/**
2+
* Definition for singly-linked list.
3+
* public class ListNode {
4+
* public int val;
5+
* public ListNode next;
6+
* public ListNode(int val=0, ListNode next=null) {
7+
* this.val = val;
8+
* this.next = next;
9+
* }
10+
* }
11+
*/
12+
public class Solution {
13+
public int GetDecimalValue(ListNode head) {
14+
int ans = 0;
15+
for (; head != null; head = head.next) {
16+
ans = ans << 1 | head.val;
17+
}
18+
return ans;
19+
}
20+
}

solution/1200-1299/1290.Convert Binary Number in a Linked List to Integer/Solution.php

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,11 @@ class Solution {
1515
* @return Integer
1616
*/
1717
function getDecimalValue($head) {
18-
$rs = [];
19-
while ($head != null) {
20-
array_push($rs, $head->val);
18+
$ans = 0;
19+
while ($head !== null) {
20+
$ans = ($ans << 1) | $head->val;
2121
$head = $head->next;
2222
}
23-
$rsStr = implode($rs);
24-
return bindec($rsStr);
23+
return $ans;
2524
}
2625
}

solution/1200-1299/1290.Convert Binary Number in a Linked List to Integer/Solution.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,11 @@
1515
// }
1616
// }
1717
impl Solution {
18-
pub fn get_decimal_value(head: Option<Box<ListNode>>) -> i32 {
18+
pub fn get_decimal_value(mut head: Option<Box<ListNode>>) -> i32 {
1919
let mut ans = 0;
20-
let mut cur = &head;
21-
while let Some(node) = cur {
20+
while let Some(node) = head {
2221
ans = (ans << 1) | node.val;
23-
cur = &node.next;
22+
head = node.next;
2423
}
2524
ans
2625
}

0 commit comments

Comments
 (0)