euisblue
1290. Convert Binary Number in a Linked List to Integer

C++

1class Solution { 2 public: 3 int getDecimalValue(ListNode* head) { 4 unsigned long result = 0; 5 6 while (true) 7 { 8 result = result << 1 + head->val; 9 head = head->next; 10 11 if (head == NULL || head == nullptr) break; 12 } 13 14 return result; 15 } 16};

JavaScript

1var getDecimalValue = function(head) { 2 let result = 0; 3 4 while (true) { 5 result = result * 2 + head.val; 6 if (head.next === null) break; 7 head = head.next; 8 } 9 10 return result; 11};

Ruby

1def get_decimal_value(head) 2 result = 0 3 4 while true do 5 result = result * 2 + head.val 6 head = head.next 7 8 break if head == nil 9 end 10 11 result 12end