-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path1290.cpp
More file actions
45 lines (36 loc) · 731 Bytes
/
1290.cpp
File metadata and controls
45 lines (36 loc) · 731 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#include<iostream>
#include<cmath>
class ListNode {
public:
ListNode *next;
int val;
ListNode(int val, ListNode *next) : val(val), next(next) {}
};
int getDecimalValue(ListNode* head) {
if(head == NULL) {
return 0;
}
int size = 0;
ListNode *temp = head;
while(temp != NULL) {
++size;
temp = temp->next;
}
if(size == 1 && head->val == 0) {
return 0;
}
int sum = 0;
int result = 0;
size = size - 1;
int i = size;
while(head != NULL) {
result += head->val * pow(2, i);
std::cout << "result: " << result << " ";
head = head->next;
--i;
}
return result;
}
int main() {
std::cin.get();
}