Given the head of a linked list head, in which each node contains an integer value.
Between every pair of adjacent nodes, insert a new node with a value equal to the greatest common divisor of them.
Return the linked list after insertion.
The greatest common divisor of two numbers is the largest positive integer that evenly divides both numbers.
Example 1:
Input: head = [18,6,10,3] Output: [18,6,6,2,10,1,3] Explanation: The 1st diagram denotes the initial linked list and the 2nd diagram denotes the linked list after inserting the new nodes (nodes in blue are the inserted nodes). - We insert the greatest common divisor of 18 and 6 = 6 between the 1st and the 2nd nodes. - We insert the greatest common divisor of 6 and 10 = 2 between the 2nd and the 3rd nodes. - We insert the greatest common divisor of 10 and 3 = 1 between the 3rd and the 4th nodes. There are no more adjacent nodes, so we return the linked list.
Example 2:
Input: head = [7] Output: [7] Explanation: The 1st diagram denotes the initial linked list and the 2nd diagram denotes the linked list after inserting the new nodes. There are no pairs of adjacent nodes, so we return the initial linked list.
Constraints:
[1, 5000].1 <= Node.val <= 1000When 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:
The brute force approach to inserting greatest common divisors (GCDs) into a linked list means we will examine every consecutive pair of numbers in the list. For each pair, we'll compute their GCD and insert it between them. We will repeat this process until we've gone through the entire list.
Here's how the algorithm would work step-by-step:
def insert_greatest_common_divisors(head):
current_node = head
while current_node and current_node.next:
first_value = current_node.val
second_value = current_node.next.val
# Calculate the GCD of the two consecutive values
greatest_common_divisor = calculate_gcd(first_value, second_value)
# Create a new node with the GCD
gcd_node = ListNode(greatest_common_divisor)
# Insert the new node between the current and next node
gcd_node.next = current_node.next
current_node.next = gcd_node
# Move to the node after the inserted GCD node
current_node = gcd_node.next
return head
def calculate_gcd(first_number, second_number):
while(second_number):
first_number, second_number = second_number, first_number % second_number
return first_number
class ListNode:
def __init__(self, val=0, next=None):
self.val = val
self.next = next
def insert_greatest_common_divisors_brute_force(head):
current_node = head
while current_node and current_node.next:
# Store values for easier use
first_value = current_node.val
second_value = current_node.next.val
# Calculate the GCD of these two numbers
greatest_common_divisor = calculate_gcd(first_value, second_value)
# Create a new node for the GCD
gcd_node = ListNode(greatest_common_divisor)
# Insert the GCD node
gcd_node.next = current_node.next
current_node.next = gcd_node
#Move to the next pair (original next and the new next)
current_node = gcd_node.next
return headThe goal is to go through the linked list and insert new nodes with the greatest common divisor (GCD) between consecutive nodes. We can do this efficiently by traversing the list once, computing the GCD on the fly, and inserting a new node after each original node.
Here's how the algorithm would work step-by-step:
def insert_greatest_common_divisors(head):
current_node = head
while current_node and current_node.next:
# Calculate GCD between current and next node value
gcd_value = calculate_gcd(current_node.val, current_node.next.val)
# Create a new node for the GCD.
gcd_node = ListNode(gcd_value)
gcd_node.next = current_node.next
current_node.next = gcd_node
# Move to the next original node.
current_node = gcd_node.next
return head
def calculate_gcd(first_number, second_number):
while second_number:
first_number, second_number = second_number, first_number % second_number
return first_number
class ListNode:
def __init__(self, val=0, next=None):
self.val = val
self.next = next| Case | How to Handle |
|---|---|
| Empty linked list | Return null immediately if the input linked list is empty. |
| Linked list with only one node | Return the original list, as no GCD can be inserted between a single node. |
| Linked list with two nodes where GCD is 1 | Ensure the solution correctly inserts a node with value 1 between the two nodes. |
| Linked list with two nodes where the numbers are equal | The GCD will be the value of the node; ensure that value is inserted correctly. |
| Large numbers in the linked list nodes that could cause integer overflow during GCD calculation | Use a GCD algorithm that avoids overflow by using subtraction/modulo operations rather than multiplication. |
| Linked list contains nodes with values of zero | The GCD of zero and any number is the number; ensure the calculation handles this case gracefully and correctly. |
| Very long linked list to ensure there's no stack overflow in recursive GCD implementations | Use an iterative GCD algorithm to avoid potential stack overflow errors with deep recursion. |
| Memory constraints when creating many new nodes in very large lists | Ensure efficient memory allocation when inserting new nodes to avoid memory exhaustion issues. |