Skip to content

Commit d8de32f

Browse files
authored
Create convert-binary-number-in-a-linked-list-to-integer.cpp
1 parent f6d47f1 commit d8de32f

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// Time: O(n)
2+
// Space: O(1)
3+
4+
/**
5+
* Definition for singly-linked list.
6+
* struct ListNode {
7+
* int val;
8+
* ListNode *next;
9+
* ListNode(int x) : val(x), next(NULL) {}
10+
* };
11+
*/
12+
class Solution {
13+
public:
14+
int getDecimalValue(ListNode* head) {
15+
int result = 0;
16+
for (; head; head = head->next) {
17+
result = result * 2 + head->val;
18+
}
19+
return result;
20+
}
21+
};

0 commit comments

Comments
 (0)