2807. Insert Greatest Common Divisors in Linked List #515
-
Topics: Given the head of a linked list 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:
Example 2:
Example 3:
Constraints:
|
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 2 replies
-
We need to insert nodes between every pair of adjacent nodes in a linked list. The value of the inserted node should be the greatest common divisor (GCD) of the two adjacent nodes' values. We'll traverse the linked list, calculate the GCD for every pair of adjacent nodes, and then insert the new node accordingly. Here's how you can approach this:
Let's implement this solution in PHP: 2807. Insert Greatest Common Divisors in Linked List <?php
// Definition for a singly-linked list node.
class ListNode {
public $val = 0;
public $next = null;
public function __construct($val = 0, $next = null) {
$this->val = $val;
$this->next = $next;
}
}
/**
* Function to calculate the GCD of two numbers.
*
* @param $a
* @param $b
* @return mixed
*/
function gcd($a, $b) {
if ($b == 0) {
return $a;
}
return gcd($b, $a % $b);
}
/**
* @param ListNode $head
* @return ListNode
*/
function insertGreatestCommonDivisors($head) {
// Edge case: If the list has only one node, return the list as is.
if ($head == null || $head->next == null) {
return $head;
}
// Start traversing the linked list from the head node.
$current = $head;
while ($current != null && $current->next != null) {
// Calculate GCD of current node's value and the next node's value.
$gcdValue = gcd($current->val, $current->next->val);
// Create a new node with the GCD value.
$gcdNode = new ListNode($gcdValue);
// Insert the GCD node between current and next node.
$gcdNode->next = $current->next;
$current->next = $gcdNode;
// Move to the node after the newly inserted GCD node.
$current = $gcdNode->next;
}
// Return the modified head of the linked list.
return $head;
}
/**
* Function to print the linked list for testing purposes.
*
* @param $head
* @return void
*/
function printList($head) {
$current = $head;
while ($current != null) {
echo $current->val . " ";
$current = $current->next;
}
echo "\n";
}
// Example usage:
// Create the linked list: 18 -> 6 -> 10 -> 3
$head = new ListNode(18);
$head->next = new ListNode(6);
$head->next->next = new ListNode(10);
$head->next->next->next = new ListNode(3);
// Insert GCD nodes.
$modifiedHead = insertGreatestCommonDivisors($head);
// Print the modified linked list.
printList($modifiedHead);
// Output should be: 18 -> 6 -> 6 -> 2 -> 10 -> 1 -> 3
?> Explanation:
Time Complexity:
Example:For the input list
|
Beta Was this translation helpful? Give feedback.
We need to insert nodes between every pair of adjacent nodes in a linked list. The value of the inserted node should be the greatest common divisor (GCD) of the two adjacent nodes' values. We'll traverse the linked list, calculate the GCD for every pair of adjacent nodes, and then insert the new node accordingly.
Here's how you can approach this:
Let's implement this soluti…