<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.
# class ListNode:
#     def __init__(self, val=0, next=None):
#         self.val = val
#         self.next = next

class Solution:
    def doubleIt(self, head: Optional[ListNode]) -> Optional[ListNode]:
        """
        This function takes the head of a singly-linked list representing a non-negative integer and doubles the integer.
        It returns the head of the new linked list that represents the doubled number.

        Time Complexity: O(n) - We traverse the linked list twice (once to convert it to an integer and once to convert it back).
        Space Complexity: O(n) - Space used for creating the new linked list.

        Steps:
        1. Traverse the linked list to convert it to an integer.
        2. Double the integer value.
        3. Convert the doubled integer back to a linked list.
        """
        
        # Helper function to convert linked list to integer
        def linked_list_to_int(node: ListNode) -> int:
            num = 0
            while node:
                num = num * 10 + node.val
                node = node.next
            return num
        
        # Helper function to convert integer to linked list
        def int_to_linked_list(num: int) -> ListNode:
            if num == 0:
                return ListNode(0)
            
            head = None
            while num:
                num, digit = divmod(num, 10)
                head = ListNode(digit, head)
            return head

        # Convert the linked list to integer
        num = linked_list_to_int(head)
        
        # Double the integer value
        doubled_num = num * 2
        
        # Convert the doubled integer back to linked list
        return int_to_linked_list(doubled_num)
# Example testing
def print_linked_list(head: ListNode):
    while head:
        print(head.val, end=' ')
        head = head.next
    print()

# Create linked list for 189
head = ListNode(1, ListNode(8, ListNode(9)))
solution = Solution()

# Test with 189
new_head = solution.doubleIt(head)
print("Doubled linked list for 189:")
print_linked_list(new_head)  # Expected output: 3 7 8

# Create linked list for 999
head = ListNode(9, ListNode(9, ListNode(9)))
new_head = solution.doubleIt(head)
print("Doubled linked list for 999:")
print_linked_list(new_head)  # Expected output: 1 9 9 8

688