Convert Binary Number in a Linked List to Integer LeetCode solution in C++

1
class Solution {
2
public:
3
int getDecimalValue(ListNode *head) {
4
ListNode *temp = head;
5
int count = 0;
6
while (temp->next != NULL) {
7
count++;
8
temp = temp->next;
9
}
10
temp = head;
11
int ans = 0;
12
while (count != -1) {
13
ans += temp->val * pow(2, count);
14
count--;
15
temp = temp->next;
16
}
17
return ans;
18
}
19
};

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0