@@ -111,6 +111,30 @@ class Solution {
111
111
}
112
112
```
113
113
114
+ ### ** C++**
115
+
116
+ ``` cpp
117
+ /* *
118
+ * Definition for singly-linked list.
119
+ * struct ListNode {
120
+ * int val;
121
+ * ListNode *next;
122
+ * ListNode(int x) : val(x), next(NULL) {}
123
+ * };
124
+ */
125
+ class Solution {
126
+ public:
127
+ int getDecimalValue(ListNode* head) {
128
+ int res = 0;
129
+ while (head != NULL) {
130
+ res = (res << 1) + head->val;
131
+ head = head->next;
132
+ }
133
+ return res;
134
+ }
135
+ };
136
+ ```
137
+
114
138
### **JavaScript**
115
139
116
140
```js
@@ -135,28 +159,30 @@ var getDecimalValue = function (head) {
135
159
};
136
160
```
137
161
138
- ### ** C++ **
162
+ ### ** TypeScript **
139
163
140
- ``` cpp
164
+ ``` ts
141
165
/**
142
166
* Definition for singly-linked list.
143
- * struct ListNode {
144
- * int val;
145
- * ListNode *next;
146
- * ListNode(int x) : val(x), next(NULL) {}
147
- * };
167
+ * class ListNode {
168
+ * val: number
169
+ * next: ListNode | null
170
+ * constructor(val?: number, next?: ListNode | null) {
171
+ * this.val = (val===undefined ? 0 : val)
172
+ * this.next = (next===undefined ? null : next)
173
+ * }
174
+ * }
148
175
*/
149
- class Solution {
150
- public:
151
- int getDecimalValue(ListNode* head) {
152
- int res = 0;
153
- while (head != NULL) {
154
- res = (res << 1) + head->val;
155
- head = head->next;
156
- }
157
- return res;
176
+
177
+ function getDecimalValue(head : ListNode | null ): number {
178
+ let ans = 0 ;
179
+ let cur = head ;
180
+ while (cur ) {
181
+ ans = (ans << 1 ) | cur .val ;
182
+ cur = cur .next ;
158
183
}
159
- };
184
+ return ans ;
185
+ }
160
186
```
161
187
162
188
### ** Rust**
@@ -180,15 +206,37 @@ public:
180
206
// }
181
207
impl Solution {
182
208
pub fn get_decimal_value (head : Option <Box <ListNode >>) -> i32 {
209
+ let mut ans = 0 ;
183
210
let mut cur = & head ;
184
- let mut res = 0;
185
211
while let Some (node ) = cur {
186
- res <<= 1;
187
- res |= node.val;
212
+ ans = (ans << 1 ) | node . val;
188
213
cur = & node . next;
189
214
}
190
- res
215
+ ans
216
+ }
217
+ }
218
+ ```
219
+
220
+ ### ** C**
221
+
222
+ ``` c
223
+ /* *
224
+ * Definition for singly-linked list.
225
+ * struct ListNode {
226
+ * int val;
227
+ * struct ListNode *next;
228
+ * };
229
+ */
230
+
231
+
232
+ int getDecimalValue (struct ListNode * head) {
233
+ int ans = 0;
234
+ struct ListNode * cur = head;
235
+ while (cur) {
236
+ ans = (ans << 1) | cur->val;
237
+ cur = cur->next;
191
238
}
239
+ return ans;
192
240
}
193
241
```
194
242
0 commit comments