File tree Expand file tree Collapse file tree 6 files changed +94
-66
lines changed
solution/1200-1299/1290.Convert Binary Number in a Linked List to Integer Expand file tree Collapse file tree 6 files changed +94
-66
lines changed Original file line number Diff line number Diff line change 76
76
77
77
class Solution:
78
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
79
+ res = 0
80
+ while head:
81
+ res = (res << 1) + head.val
82
+ head = head.next
89
83
return res
90
84
```
91
85
@@ -104,16 +98,10 @@ class Solution:
104
98
*/
105
99
class Solution {
106
100
public int getDecimalValue(ListNode head) {
107
- int n = 0;
108
- for (ListNode cur = head; cur != null; cur = cur.next) {
109
- ++n;
110
- }
111
101
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
- }
102
+ while (head != null) {
103
+ res = (res << 1) + head.val;
104
+ head = head.next;
117
105
}
118
106
return res;
119
107
}
@@ -136,15 +124,38 @@ class Solution {
136
124
*/
137
125
var getDecimalValue = function(head) {
138
126
let res = 0;
139
- while (head !== null) {
140
- res *= 2;
141
- if (head.val) res += 1;
127
+ while (head != null) {
128
+ res = (res << 1) + head.val;
142
129
head = head.next;
143
130
}
144
131
return res;
145
132
};
146
133
```
147
134
135
+ ### **C++**
136
+
137
+ ```cpp
138
+ /**
139
+ * Definition for singly-linked list.
140
+ * struct ListNode {
141
+ * int val;
142
+ * ListNode *next;
143
+ * ListNode(int x) : val(x), next(NULL) {}
144
+ * };
145
+ */
146
+ class Solution {
147
+ public:
148
+ int getDecimalValue(ListNode* head) {
149
+ int res = 0;
150
+ while (head != NULL) {
151
+ res = (res << 1) + head->val;
152
+ head = head->next;
153
+ }
154
+ return res;
155
+ }
156
+ };
157
+ ```
158
+
148
159
### **...**
149
160
150
161
```
Original file line number Diff line number Diff line change 89
89
90
90
class Solution:
91
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
92
+ res = 0
93
+ while head:
94
+ res = (res << 1) + head.val
95
+ head = head.next
102
96
return res
103
97
```
104
98
@@ -115,16 +109,10 @@ class Solution:
115
109
*/
116
110
class Solution {
117
111
public int getDecimalValue(ListNode head) {
118
- int n = 0;
119
- for (ListNode cur = head; cur != null; cur = cur.next) {
120
- ++n;
121
- }
122
112
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
- }
113
+ while (head != null) {
114
+ res = (res << 1) + head.val;
115
+ head = head.next;
128
116
}
129
117
return res;
130
118
}
@@ -147,15 +135,38 @@ class Solution {
147
135
*/
148
136
var getDecimalValue = function(head) {
149
137
let res = 0;
150
- while (head !== null) {
151
- res *= 2;
152
- if (head.val) res += 1;
138
+ while (head != null) {
139
+ res = (res << 1) + head.val;
153
140
head = head.next;
154
141
}
155
142
return res;
156
143
};
157
144
```
158
145
146
+ ### **C++**
147
+
148
+ ```cpp
149
+ /**
150
+ * Definition for singly-linked list.
151
+ * struct ListNode {
152
+ * int val;
153
+ * ListNode *next;
154
+ * ListNode(int x) : val(x), next(NULL) {}
155
+ * };
156
+ */
157
+ class Solution {
158
+ public:
159
+ int getDecimalValue(ListNode* head) {
160
+ int res = 0;
161
+ while (head != NULL) {
162
+ res = (res << 1) + head->val;
163
+ head = head->next;
164
+ }
165
+ return res;
166
+ }
167
+ };
168
+ ```
169
+
159
170
### **...**
160
171
161
172
```
Original file line number Diff line number Diff line change
1
+ /**
2
+ * Definition for singly-linked list.
3
+ * struct ListNode {
4
+ * int val;
5
+ * ListNode *next;
6
+ * ListNode(int x) : val(x), next(NULL) {}
7
+ * };
8
+ */
9
+ class Solution {
10
+ public:
11
+ int getDecimalValue(ListNode* head) {
12
+ int res = 0;
13
+ while (head != NULL) {
14
+ res = (res << 1) + head->val;
15
+ head = head->next;
16
+ }
17
+ return res;
18
+ }
19
+ };
Original file line number Diff line number Diff line change 8
8
*/
9
9
class Solution {
10
10
public int getDecimalValue(ListNode head) {
11
- int n = 0;
12
- for (ListNode cur = head; cur != null; cur = cur.next) {
13
- ++n;
14
- }
15
11
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);
20
- }
12
+ while (head != null) {
13
+ res = (res << 1) + head.val;
14
+ head = head.next;
21
15
}
22
16
return res;
23
17
}
Original file line number Diff line number Diff line change 11
11
*/
12
12
var getDecimalValue = function(head) {
13
13
let res = 0;
14
- while (head !== null) {
15
- res *= 2;
16
- if (head.val) res += 1;
14
+ while (head != null) {
15
+ res = (res << 1) + head.val;
17
16
head = head.next;
18
17
}
19
18
return res;
Original file line number Diff line number Diff line change 6
6
7
7
class Solution:
8
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
9
+ res = 0
10
+ while head:
11
+ res = (res << 1) + head.val
12
+ head = head.next
19
13
return res
You can’t perform that action at this time.
0 commit comments