You are given the head of a non-empty linked list representing a non-negative integer without leading zeroes.
Return the head of the linked list after doubling it.
Example 1:
Input: head = [1,8,9] Output: [3,7,8] Explanation: 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.
Example 2:
Input: head = [9,9,9] Output: [1,9,9,8] Explanation: 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.
Constraints:
[1, 104]0 <= Node.val <= 90 itself.When you get asked this question in a real-life environment, it will often be ambiguous (especially at FAANG). Make sure to ask these questions in that case:
We're given a number represented as a linked list, and we want to double that number. The brute force method is like converting the number to its usual decimal form, doubling it, and then converting it back to a linked list.
Here's how the algorithm would work step-by-step:
class ListNode:
def __init__(self, value=0, next_node=None):
self.value = value
self.next_node = next_node
def double_linked_list_brute_force(head):
# Convert the linked list to a number.
current_node = head
number_as_string = ""
while current_node:
number_as_string += str(current_node.value)
current_node = current_node.next_node
if not number_as_string:
return None
number = int(number_as_string)
doubled_number = number * 2
# Convert the doubled number back to a linked list.
doubled_number_string = str(doubled_number)
new_head = ListNode(int(doubled_number_string[0]))
current_node = new_head
# Build the new linked list
for digit_index in range(1, len(doubled_number_string)):
digit = int(doubled_number_string[digit_index])
new_node = ListNode(digit)
current_node.next_node = new_node
current_node = new_node
return new_headWe want to double a number that's stored as a linked list, one digit per node. The key idea is to simulate the multiplication process we learned in elementary school, working from right to left, handling carries along the way. This approach efficiently updates the list in place, representing the doubled number.
Here's how the algorithm would work step-by-step:
class ListNode:
def __init__(self, val=0, next=None):
self.val = val
self.next = next
def double_linked_list(head):
current_node = head
number_list = []
while current_node:
number_list.append(current_node)
current_node = current_node.next
carry = 0
for i in range(len(number_list) - 1, -1, -1):
current_digit = number_list[i].val
doubled_digit = 2 * current_digit + carry
# Handle the carry-over if the doubled digit is greater than 9.
number_list[i].val = doubled_digit % 10
carry = doubled_digit // 10
# If carry remains after doubling the most significant digit,
if carry:
new_head = ListNode(carry)
new_head.next = head
head = new_head
return head| Case | How to Handle |
|---|---|
| Empty linked list as input | Return a new linked list with a single node containing 0. |
| Linked list representing a single-digit number | Multiply the single digit by 2, handle carry if any, and create the new linked list accordingly. |
| Linked list representing a number with leading zeros | The algorithm should correctly process the linked list without being affected by leading zeros, preserving their place value during multiplication. |
| Large number represented by the linked list that could lead to integer overflow during multiplication | The digit-by-digit multiplication and carry-over method prevents integer overflow, as it processes digits individually. |
| Linked list representing a number with many digits (scalability) | The solution's iterative approach scales linearly with the number of digits, making it efficient for large numbers. |
| Carry-over occurring in the most significant digit | Append a new node with the carry value to the beginning of the result linked list if the final carry is non-zero. |
| Linked list representing the number 0 | Return a new linked list with a single node containing 0. |
| Negative numbers represented in linked list format. | The problem context assumes a positive number, therefore a negative input is invalid, and should be addressed by returning an error or defining that negative numbers should be converted to positive. |