1290. 二进制链表转整数

 1 /**
 2  * Definition for singly-linked list.
 3  * struct ListNode {
 4  *     int val;
 5  *     struct ListNode *next;
 6  * };
 7  */
 8 
 9 
10 int getDecimalValue(struct ListNode* head){
11     int count = 0;
12     struct ListNode* p = head;
13     while(p)
14     {
15         count++;
16         p = p->next;
17     }
18 
19     int sum = 0;
20     count--;
21     while(head)
22     {
23             sum += (head->val )*(int)(pow(2,count)); 
24             head = head->next;
25             count--;
26     }
27 
28 
29     return sum;
30 }

 

posted @ 2021-06-16 16:18  传说中的旅行者  阅读(46)  评论(0)    收藏  举报