1290. Convert Binary Number in a Linked List to Integer 發表於 2023-02-13 | 分類於 leetcode problemsolutionoption 1 - recursive1234567891011121314class Solution {public: int ret = 0; void preorder(ListNode * head){ if(!head) return ; ret<<=1; ret+=head->val; preorder(head->next); } int getDecimalValue(ListNode* head) { preorder(head); return ret; }}; option 2 - iterative1234567891011class Solution {public: int getDecimalValue(ListNode* head) { int sum = 0; for(;head;head=head->next){ sum<<=1; sum+=head->val; } return sum; }}; analysis time complexity O(n) space complexity O(1)