Skip to content

Commit 430a041

Browse files
committed
feat: add solutions to lc problem: No.1290. Convert Binary Number in a Linked List to Integer
1 parent 22a60cf commit 430a041

File tree

4 files changed

+117
-37
lines changed

4 files changed

+117
-37
lines changed

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

+45-3
Original file line numberDiff line numberDiff line change
@@ -55,27 +55,69 @@
5555
<li>每个结点的值不是&nbsp;<code>0</code> 就是 <code>1</code>。</li>
5656
</ul>
5757

58-
5958
## 解法
6059

6160
<!-- 这里可写通用的实现逻辑 -->
6261

62+
先求链表长度 n,再遍历链表,累加求得结果。
63+
6364
<!-- tabs:start -->
6465

6566
### **Python3**
6667

6768
<!-- 这里可写当前语言的特殊实现逻辑 -->
6869

6970
```python
70-
71+
# Definition for singly-linked list.
72+
# class ListNode:
73+
# def __init__(self, x):
74+
# self.val = x
75+
# self.next = None
76+
77+
class Solution:
78+
def getDecimalValue(self, head: ListNode) -> int:
79+
n, cur = 0, head
80+
while cur:
81+
n += 1
82+
cur = cur.next
83+
res, cur = 0, head
84+
while cur:
85+
n -= 1
86+
if cur.val == 1:
87+
res += (2 ** (n))
88+
cur = cur.next
89+
return res
7190
```
7291

7392
### **Java**
7493

7594
<!-- 这里可写当前语言的特殊实现逻辑 -->
7695

7796
```java
78-
97+
/**
98+
* Definition for singly-linked list.
99+
* public class ListNode {
100+
* int val;
101+
* ListNode next;
102+
* ListNode(int x) { val = x; }
103+
* }
104+
*/
105+
class Solution {
106+
public int getDecimalValue(ListNode head) {
107+
int n = 0;
108+
for (ListNode cur = head; cur != null; cur = cur.next) {
109+
++n;
110+
}
111+
int res = 0;
112+
for (ListNode cur = head; cur != null; cur = cur.next) {
113+
--n;
114+
if (cur.val == 1) {
115+
res += Math.pow(2, n);
116+
}
117+
}
118+
return res;
119+
}
120+
}
79121
```
80122

81123
### **...**

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

+43-26
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,8 @@
66

77
<p>Given <code>head</code> which is a reference node to&nbsp;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.</p>
88

9-
10-
119
<p>Return the <em>decimal value</em> of the number in the linked list.</p>
1210

13-
14-
1511
<p>&nbsp;</p>
1612

1713
<p><strong>Example 1:</strong></p>
@@ -28,12 +24,8 @@
2824

2925
</pre>
3026

31-
32-
3327
<p><strong>Example 2:</strong></p>
3428

35-
36-
3729
<pre>
3830

3931
<strong>Input:</strong> head = [0]
@@ -42,12 +34,8 @@
4234

4335
</pre>
4436

45-
46-
4737
<p><strong>Example 3:</strong></p>
4838

49-
50-
5139
<pre>
5240

5341
<strong>Input:</strong> head = [1]
@@ -56,12 +44,8 @@
5644

5745
</pre>
5846

59-
60-
6147
<p><strong>Example 4:</strong></p>
6248

63-
64-
6549
<pre>
6650

6751
<strong>Input:</strong> head = [1,0,0,1,0,0,1,1,1,0,0,0,0,0,0]
@@ -70,12 +54,8 @@
7054

7155
</pre>
7256

73-
74-
7557
<p><strong>Example 5:</strong></p>
7658

77-
78-
7959
<pre>
8060

8161
<strong>Input:</strong> head = [0,0]
@@ -84,14 +64,10 @@
8464

8565
</pre>
8666

87-
88-
8967
<p>&nbsp;</p>
9068

9169
<p><strong>Constraints:</strong></p>
9270

93-
94-
9571
<ul>
9672
<li>The Linked List is not empty.</li>
9773
<li>Number of nodes&nbsp;will not exceed <code>30</code>.</li>
@@ -105,13 +81,54 @@
10581
### **Python3**
10682

10783
```python
108-
84+
# Definition for singly-linked list.
85+
# class ListNode:
86+
# def __init__(self, x):
87+
# self.val = x
88+
# self.next = None
89+
90+
class Solution:
91+
def getDecimalValue(self, head: ListNode) -> int:
92+
n, cur = 0, head
93+
while cur:
94+
n += 1
95+
cur = cur.next
96+
res, cur = 0, head
97+
while cur:
98+
n -= 1
99+
if cur.val == 1:
100+
res += (2 ** (n))
101+
cur = cur.next
102+
return res
109103
```
110104

111105
### **Java**
112106

113107
```java
114-
108+
/**
109+
* Definition for singly-linked list.
110+
* public class ListNode {
111+
* int val;
112+
* ListNode next;
113+
* ListNode(int x) { val = x; }
114+
* }
115+
*/
116+
class Solution {
117+
public int getDecimalValue(ListNode head) {
118+
int n = 0;
119+
for (ListNode cur = head; cur != null; cur = cur.next) {
120+
++n;
121+
}
122+
int res = 0;
123+
for (ListNode cur = head; cur != null; cur = cur.next) {
124+
--n;
125+
if (cur.val == 1) {
126+
res += Math.pow(2, n);
127+
}
128+
}
129+
return res;
130+
}
131+
}
115132
```
116133

117134
### **...**

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

+10-8
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,17 @@
88
*/
99
class Solution {
1010
public int getDecimalValue(ListNode head) {
11-
int sum = 0;
12-
StringBuilder sb = new StringBuilder("0");
13-
while (head != null) {
14-
sum += head.val;
15-
if (sum != 0) {
16-
sb.append(head.val);
11+
int n = 0;
12+
for (ListNode cur = head; cur != null; cur = cur.next) {
13+
++n;
14+
}
15+
int res = 0;
16+
for (ListNode cur = head; cur != null; cur = cur.next) {
17+
--n;
18+
if (cur.val == 1) {
19+
res += Math.pow(2, n);
1720
}
18-
head = head.next;
1921
}
20-
return Integer.valueOf(sb.toString(), 2);
22+
return res;
2123
}
2224
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# Definition for singly-linked list.
2+
# class ListNode:
3+
# def __init__(self, x):
4+
# self.val = x
5+
# self.next = None
6+
7+
class Solution:
8+
def getDecimalValue(self, head: ListNode) -> int:
9+
n, cur = 0, head
10+
while cur:
11+
n += 1
12+
cur = cur.next
13+
res, cur = 0, head
14+
while cur:
15+
n -= 1
16+
if cur.val == 1:
17+
res += (2 ** (n))
18+
cur = cur.next
19+
return res

0 commit comments

Comments
 (0)