Skip to content

Commit a1fa282

Browse files
committed
Added file 1290.cpp
1 parent 07e254f commit a1fa282

File tree

1 file changed

+28
-0
lines changed

1 file changed

+28
-0
lines changed

leetcode/cpp/linkedlist/1290.cpp

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/**
2+
* Definition for singly-linked list.
3+
* struct ListNode {
4+
* int val;
5+
* ListNode *next;
6+
* ListNode() : val(0), next(nullptr) {}
7+
* ListNode(int x) : val(x), next(nullptr) {}
8+
* ListNode(int x, ListNode *next) : val(x), next(next) {}
9+
* };
10+
*/
11+
class Solution {
12+
public:
13+
int getDecimalValue(ListNode* head) {
14+
int answer = 0;
15+
vector<int> v;
16+
while(head){
17+
v.push_back(head->val);
18+
head = head->next;
19+
}
20+
reverse(v.begin(),v.end());
21+
for (int i = 0; i < v.size(); i++)
22+
if(v.at(i))
23+
answer += pow(2,i);
24+
return answer;
25+
}
26+
};
27+
28+

0 commit comments

Comments
 (0)