Skip to content

Commit bb50ac7

Browse files
committed
feat: add solutions to lc problem: No.1290
1 parent d6f98d9 commit bb50ac7

File tree

8 files changed

+142
-98
lines changed

8 files changed

+142
-98
lines changed

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

Lines changed: 53 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -59,9 +59,13 @@
5959

6060
<!-- 这里可写通用的实现逻辑 -->
6161

62-
遍历链表
62+
**方法一:遍历链表**
6363

64-
当遍历到链表某个结点,先将已有结果 res 乘以 2(即左移 1 位:`<< 1`),再加上当前结点的值,得出已遍历过的结点的十进制值。最后返回 res 即可。
64+
我们用变量 `ans` 记录当前的十进制值,初始值为 $0$。
65+
66+
遍历链表,对于每个结点,将 `ans` 左移一位,然后再或上当前结点的值。遍历结束后,`ans` 即为十进制值。
67+
68+
时间复杂度 $O(n)$,空间复杂度 $O(1)$。其中 $n$ 为链表的长度。
6569

6670
<!-- tabs:start -->
6771

@@ -72,18 +76,16 @@
7276
```python
7377
# Definition for singly-linked list.
7478
# class ListNode:
75-
# def __init__(self, x):
76-
# self.val = x
77-
# self.next = None
78-
79-
79+
# def __init__(self, val=0, next=None):
80+
# self.val = val
81+
# self.next = next
8082
class Solution:
8183
def getDecimalValue(self, head: ListNode) -> int:
82-
res = 0
84+
ans = 0
8385
while head:
84-
res = (res << 1) + head.val
86+
ans = ans << 1 | head.val
8587
head = head.next
86-
return res
88+
return ans
8789
```
8890

8991
### **Java**
@@ -96,17 +98,18 @@ class Solution:
9698
* public class ListNode {
9799
* int val;
98100
* ListNode next;
99-
* ListNode(int x) { val = x; }
101+
* ListNode() {}
102+
* ListNode(int val) { this.val = val; }
103+
* ListNode(int val, ListNode next) { this.val = val; this.next = next; }
100104
* }
101105
*/
102106
class Solution {
103107
public int getDecimalValue(ListNode head) {
104-
int res = 0;
105-
while (head != null) {
106-
res = (res << 1) + head.val;
107-
head = head.next;
108+
int ans = 0;
109+
for (; head != null; head = head.next) {
110+
ans = ans << 1 | head.val;
108111
}
109-
return res;
112+
return ans;
110113
}
111114
}
112115
```
@@ -119,43 +122,61 @@ class Solution {
119122
* struct ListNode {
120123
* int val;
121124
* ListNode *next;
122-
* ListNode(int x) : val(x), next(NULL) {}
125+
* ListNode() : val(0), next(nullptr) {}
126+
* ListNode(int x) : val(x), next(nullptr) {}
127+
* ListNode(int x, ListNode *next) : val(x), next(next) {}
123128
* };
124129
*/
125130
class Solution {
126131
public:
127132
int getDecimalValue(ListNode* head) {
128-
int res = 0;
129-
while (head != NULL) {
130-
res = (res << 1) + head->val;
131-
head = head->next;
133+
int ans = 0;
134+
for (; head; head = head->next) {
135+
ans = ans << 1 | head->val;
132136
}
133-
return res;
137+
return ans;
134138
}
135139
};
136140
```
137141
142+
### **Go**
143+
144+
```go
145+
/**
146+
* Definition for singly-linked list.
147+
* type ListNode struct {
148+
* Val int
149+
* Next *ListNode
150+
* }
151+
*/
152+
func getDecimalValue(head *ListNode) (ans int) {
153+
for ; head != nil; head = head.Next {
154+
ans = ans<<1 | head.Val
155+
}
156+
return
157+
}
158+
```
159+
138160
### **JavaScript**
139161

140162
```js
141163
/**
142164
* Definition for singly-linked list.
143-
* function ListNode(val) {
144-
* this.val = val;
145-
* this.next = null;
165+
* function ListNode(val, next) {
166+
* this.val = (val===undefined ? 0 : val)
167+
* this.next = (next===undefined ? null : next)
146168
* }
147169
*/
148170
/**
149171
* @param {ListNode} head
150172
* @return {number}
151173
*/
152174
var getDecimalValue = function (head) {
153-
let res = 0;
154-
while (head != null) {
155-
res = (res << 1) + head.val;
156-
head = head.next;
175+
let ans = 0;
176+
for (; head; head = head.next) {
177+
ans = (ans << 1) | head.val;
157178
}
158-
return res;
179+
return ans;
159180
};
160181
```
161182

@@ -176,10 +197,8 @@ var getDecimalValue = function (head) {
176197

177198
function getDecimalValue(head: ListNode | null): number {
178199
let ans = 0;
179-
let cur = head;
180-
while (cur) {
181-
ans = (ans << 1) | cur.val;
182-
cur = cur.next;
200+
for (; head; head = head.next) {
201+
ans = (ans << 1) | head.val;
183202
}
184203
return ans;
185204
}

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

Lines changed: 47 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -44,18 +44,16 @@
4444
```python
4545
# Definition for singly-linked list.
4646
# class ListNode:
47-
# def __init__(self, x):
48-
# self.val = x
49-
# self.next = None
50-
51-
47+
# def __init__(self, val=0, next=None):
48+
# self.val = val
49+
# self.next = next
5250
class Solution:
5351
def getDecimalValue(self, head: ListNode) -> int:
54-
res = 0
52+
ans = 0
5553
while head:
56-
res = (res << 1) + head.val
54+
ans = ans << 1 | head.val
5755
head = head.next
58-
return res
56+
return ans
5957
```
6058

6159
### **Java**
@@ -66,17 +64,18 @@ class Solution:
6664
* public class ListNode {
6765
* int val;
6866
* ListNode next;
69-
* ListNode(int x) { val = x; }
67+
* ListNode() {}
68+
* ListNode(int val) { this.val = val; }
69+
* ListNode(int val, ListNode next) { this.val = val; this.next = next; }
7070
* }
7171
*/
7272
class Solution {
7373
public int getDecimalValue(ListNode head) {
74-
int res = 0;
75-
while (head != null) {
76-
res = (res << 1) + head.val;
77-
head = head.next;
74+
int ans = 0;
75+
for (; head != null; head = head.next) {
76+
ans = ans << 1 | head.val;
7877
}
79-
return res;
78+
return ans;
8079
}
8180
}
8281
```
@@ -89,43 +88,61 @@ class Solution {
8988
* struct ListNode {
9089
* int val;
9190
* ListNode *next;
92-
* ListNode(int x) : val(x), next(NULL) {}
91+
* ListNode() : val(0), next(nullptr) {}
92+
* ListNode(int x) : val(x), next(nullptr) {}
93+
* ListNode(int x, ListNode *next) : val(x), next(next) {}
9394
* };
9495
*/
9596
class Solution {
9697
public:
9798
int getDecimalValue(ListNode* head) {
98-
int res = 0;
99-
while (head != NULL) {
100-
res = (res << 1) + head->val;
101-
head = head->next;
99+
int ans = 0;
100+
for (; head; head = head->next) {
101+
ans = ans << 1 | head->val;
102102
}
103-
return res;
103+
return ans;
104104
}
105105
};
106106
```
107107
108+
### **Go**
109+
110+
```go
111+
/**
112+
* Definition for singly-linked list.
113+
* type ListNode struct {
114+
* Val int
115+
* Next *ListNode
116+
* }
117+
*/
118+
func getDecimalValue(head *ListNode) (ans int) {
119+
for ; head != nil; head = head.Next {
120+
ans = ans<<1 | head.Val
121+
}
122+
return
123+
}
124+
```
125+
108126
### **JavaScript**
109127

110128
```js
111129
/**
112130
* Definition for singly-linked list.
113-
* function ListNode(val) {
114-
* this.val = val;
115-
* this.next = null;
131+
* function ListNode(val, next) {
132+
* this.val = (val===undefined ? 0 : val)
133+
* this.next = (next===undefined ? null : next)
116134
* }
117135
*/
118136
/**
119137
* @param {ListNode} head
120138
* @return {number}
121139
*/
122140
var getDecimalValue = function (head) {
123-
let res = 0;
124-
while (head != null) {
125-
res = (res << 1) + head.val;
126-
head = head.next;
141+
let ans = 0;
142+
for (; head; head = head.next) {
143+
ans = (ans << 1) | head.val;
127144
}
128-
return res;
145+
return ans;
129146
};
130147
```
131148

@@ -146,10 +163,8 @@ var getDecimalValue = function (head) {
146163

147164
function getDecimalValue(head: ListNode | null): number {
148165
let ans = 0;
149-
let cur = head;
150-
while (cur) {
151-
ans = (ans << 1) | cur.val;
152-
cur = cur.next;
166+
for (; head; head = head.next) {
167+
ans = (ans << 1) | head.val;
153168
}
154169
return ans;
155170
}

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

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,18 @@
33
* struct ListNode {
44
* int val;
55
* ListNode *next;
6-
* ListNode(int x) : val(x), next(NULL) {}
6+
* ListNode() : val(0), next(nullptr) {}
7+
* ListNode(int x) : val(x), next(nullptr) {}
8+
* ListNode(int x, ListNode *next) : val(x), next(next) {}
79
* };
810
*/
911
class Solution {
1012
public:
1113
int getDecimalValue(ListNode* head) {
12-
int res = 0;
13-
while (head != NULL) {
14-
res = (res << 1) + head->val;
15-
head = head->next;
14+
int ans = 0;
15+
for (; head; head = head->next) {
16+
ans = ans << 1 | head->val;
1617
}
17-
return res;
18+
return ans;
1819
}
1920
};
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
/**
2+
* Definition for singly-linked list.
3+
* type ListNode struct {
4+
* Val int
5+
* Next *ListNode
6+
* }
7+
*/
8+
func getDecimalValue(head *ListNode) (ans int) {
9+
for ; head != nil; head = head.Next {
10+
ans = ans<<1 | head.Val
11+
}
12+
return
13+
}

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

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,17 @@
33
* public class ListNode {
44
* int val;
55
* ListNode next;
6-
* ListNode(int x) { val = x; }
6+
* ListNode() {}
7+
* ListNode(int val) { this.val = val; }
8+
* ListNode(int val, ListNode next) { this.val = val; this.next = next; }
79
* }
810
*/
911
class Solution {
1012
public int getDecimalValue(ListNode head) {
11-
int res = 0;
12-
while (head != null) {
13-
res = (res << 1) + head.val;
14-
head = head.next;
13+
int ans = 0;
14+
for (; head != null; head = head.next) {
15+
ans = ans << 1 | head.val;
1516
}
16-
return res;
17+
return ans;
1718
}
1819
}
Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,18 @@
11
/**
22
* Definition for singly-linked list.
3-
* function ListNode(val) {
4-
* this.val = val;
5-
* this.next = null;
3+
* function ListNode(val, next) {
4+
* this.val = (val===undefined ? 0 : val)
5+
* this.next = (next===undefined ? null : next)
66
* }
77
*/
88
/**
99
* @param {ListNode} head
1010
* @return {number}
1111
*/
1212
var getDecimalValue = function (head) {
13-
let res = 0;
14-
while (head != null) {
15-
res = (res << 1) + head.val;
16-
head = head.next;
13+
let ans = 0;
14+
for (; head; head = head.next) {
15+
ans = (ans << 1) | head.val;
1716
}
18-
return res;
17+
return ans;
1918
};

0 commit comments

Comments
 (0)