diff --git a/1290. Convert Binary Number in a Linked List to Integer b/1290. Convert Binary Number in a Linked List to Integer new file mode 100644 index 0000000..de75f19 --- /dev/null +++ b/1290. Convert Binary Number in a Linked List to Integer @@ -0,0 +1,27 @@ +/** + * Definition for singly-linked list. + * struct ListNode { + * int val; + * ListNode *next; + * ListNode() : val(0), next(nullptr) {} + * ListNode(int x) : val(x), next(nullptr) {} + * ListNode(int x, ListNode *next) : val(x), next(next) {} + * }; + */ +class Solution { +public: + int getDecimalValue(ListNode* head) { + int result = 0; // Initialize result to store decimal value + + // Traverse the linked list from head to tail + while (head != nullptr) { + // Shift result left by 1 bit (multiply by 2) and add current bit + result = result * 2 + head->val; + + // Move to the next node + head = head->next; + } + + return result; // Return the final decimal value + } +};