题目
链接
详情
实例
提示
题解
思路
遍历链表,获取链表的值添加到容器内
在容器内遍历值,由高位到地位遍历,为权重,然后算值
代码
/**
* 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:
int getDecimalValue(ListNode* head) {
int iRet = 0;
vector<int> vec;
while (nullptr != head)
{
vec.push_back(head->val);//获取值
head = head->next;//下一指针
}
size_t iSize = vec.size();
for (int i = iSize - 1; i >= 0; i--)
iRet += vec.at(i) * pow(2, iSize - 1 - i);
return iRet;
}
};
标签:head,ListNode,val,int,next,链表,1290,LeetCode
From: https://www.cnblogs.com/EricsT/p/18554845