From a62ddd7b8f2072afd280f28b90cfc974c043bea1 Mon Sep 17 00:00:00 2001 From: chayan das Date: Mon, 14 Jul 2025 13:49:18 +0530 Subject: [PATCH] Create 1290. Convert Binary Number in a Linked List to Integer --- ... Binary Number in a Linked List to Integer | 27 +++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 1290. Convert Binary Number in a Linked List to Integer 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 + } +};