-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path1290.二进制链表转整数.cpp
56 lines (54 loc) · 1.1 KB
/
1290.二进制链表转整数.cpp
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
50
51
52
53
54
55
56
/*
* @lc app=leetcode.cn id=1290 lang=cpp
*
* [1290] 二进制链表转整数
*/
// @lc code=start
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode() : val(0), next(nullptr) {}
* ListNode(int x) : val(x), next(nullptr) {}
* ListNode(int x, ListNode *next) : val(x), next(next) {}
* };
*/
class Solution {
public:
// 100 42;
int getDecimalValue(ListNode* head) {
ListNode* p = head;
int sum = 0;
while(p){
sum = (sum << 1) + p->val;
p = p->next;
}
return sum;
}
int getDecimalValue1(ListNode* head) {
ListNode* p = head;
int size = 0;
while(p){
p = p->next;
++size;
}
--size;
int sum = 0;
p = head;
while(p){
sum += p->val << size;
p = p->next;
--size;
}
return sum;
}
};
// @lc code=end
/*
[1,0,1]\n
[0]\n
[1]\n
[1,0,0,1,0,0,1,1,1,0,0,0,0,0,0]\n
[0,0]
*/