<p>You are given the <code>head</code> of a <strong>non-empty</strong> linked list representing a non-negative integer without leading zeroes.</p>

<p>Return <em>the </em><code>head</code><em> of the linked list after <strong>doubling</strong> it</em>.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2023/05/28/example.png" style="width: 401px; height: 81px;" />
<pre>
<strong>Input:</strong> head = [1,8,9]
<strong>Output:</strong> [3,7,8]
<strong>Explanation:</strong> The figure above corresponds to the given linked list which represents the number 189. Hence, the returned linked list represents the number 189 * 2 = 378.
</pre>

<p><strong class="example">Example 2:</strong></p>
<img alt="" src="https://assets.leetcode.com/uploads/2023/05/28/example2.png" style="width: 401px; height: 81px;" />
<pre>
<strong>Input:</strong> head = [9,9,9]
<strong>Output:</strong> [1,9,9,8]
<strong>Explanation:</strong> The figure above corresponds to the given linked list which represents the number 999. Hence, the returned linked list reprersents the number 999 * 2 = 1998. 
</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li>The number of nodes in the list is in the range <code>[1, 10<sup>4</sup>]</code></li>
	<li><font face="monospace"><code>0 &lt;= Node.val &lt;= 9</code></font></li>
	<li>The input is generated such that the list represents a number that does not have leading zeros, except the number <code>0</code> itself.</li>
</ul>

// /**
//  * 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:
//     ListNode* doubleIt(ListNode* head) {
//         vector<int>v1;
//         ListNode*curr = head;
//         string str1 = " ";
//         while(curr != nullptr)
//         {
//            str1+= to_string(curr->val);
//             curr = curr->next;
//         }
//         long long numb = stoll(str1);

//         // v1.pop();
//         numb = numb * 2;
//         string str = to_string(numb);
//         for (int i = 0; i < str.length(); i++) {
//             v1.push_back(str[i] - '0');
//         }
//         // v1.push_back(numb);
//         ListNode* l1 = new ListNode(0);
//         ListNode*newhead = l1;
//         for(int i = 0 ; i < v1.size() ; i++)
//         {
//             newhead->next = new ListNode(v1[i]);
//             newhead = newhead->next;
//         }
//         return l1->next;

//     }
// };

class Solution {
public:
    ListNode* doubleIt(ListNode* head) {
        if (!head) return nullptr;

        // Convert linked list to a string representing the number
        string str1;
        ListNode* curr = head;
        while (curr != nullptr) {
            str1 += to_string(curr->val);
            curr = curr->next;
        }

        // Double the number by treating it as a string to handle large numbers
        string doubled = multiplyByTwo(str1);

        // Create a new linked list from the doubled string
        ListNode* dummy = new ListNode(0);
        ListNode* newHead = dummy;
        for (char c : doubled) {
            newHead->next = new ListNode(c - '0');
            newHead = newHead->next;
        }

        return dummy->next;
    }

private:
    string multiplyByTwo(const string& num) {
        string result;
        int carry = 0;
        for (int i = num.size() - 1; i >= 0; --i) {
            int digit = (num[i] - '0') * 2 + carry;
            carry = digit / 10;
            result.push_back((digit % 10) + '0');
        }
        if (carry) {
            result.push_back(carry + '0');
        }
        reverse(result.begin(), result.end());
        return result;
    }
};
124595