-
-
Notifications
You must be signed in to change notification settings - Fork 291
Expand file tree
/
Copy path1290.cpp
More file actions
49 lines (48 loc) · 1.17 KB
/
1290.cpp
File metadata and controls
49 lines (48 loc) · 1.17 KB
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
45
46
47
48
49
__________________________________________________________________________________________________
sample 0 ms submission
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
int getDecimalValue(ListNode* head) {
int ans=0;
int i=0;
ListNode *prev=NULL,*curr=head,*next=head;
while(next!=NULL)
{
next=curr->next;
curr->next=prev;
prev=curr;
curr=next;
}
while(prev!=NULL)
{
ans+=((prev->val)*pow(2,i));
i++;
prev=prev->next;
}
return ans;
}
};
__________________________________________________________________________________________________
4ms
class Solution {
public:
int getDecimalValue(ListNode* head) {
int ret = 0;
while(head)
{
ret <<= 1;
ret |= head->val;
head = head->next;
}
return ret;
}
};
__________________________________________________________________________________________________