
/**
* 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 nums = 0;
ListNode* cur=head;
while(head!=nullptr){
nums=2*nums+head->val;
head=head->next;
}
return nums;
}
};
本文介绍了一种算法,该算法将一个单链表中的每个节点值视为二进制位,从链表头部到尾部依次读取,最终转换为对应的十进制整数值。
336

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



