/**
* 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) {
string s="";
while(head)
{
s+=(int)(head->val)+'0';
head=head->next;
}
cout<<s;
int res=0;
for(int i=0;i<s.size();i++)
{
res+=(s[i]-'0')*pow(2,s.size()-1-i);
}
return res;
}
};
LeetCode:1290. 二进制链表转整数
最新推荐文章于 2025-07-14 23:53:27 发布
本文介绍了一种将链表中的整数转换为二进制数并计算其十进制值的方法。通过遍历链表,将每个节点的值转换为字符并拼接成字符串,再从字符串中读取每个字符,将其转换回数字并乘以2的相应次方,最终累加得到十进制数值。
335

被折叠的 条评论
为什么被折叠?



