Closed mah-shamim closed 1 week ago
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
?>
ListNode Class: This class represents the structure of a node in the linked list, with properties for the value ($val
) and the next node ($next
).
GCD Calculation: The gcd
function uses the Euclidean algorithm to compute the greatest common divisor of two integers.
Main Logic (insertGreatestCommonDivisors
):
Edge Case: If the list has only one node, we return it as is without making any changes since there are no adjacent nodes.
Testing: The function printList
is a helper function used to output the values of the linked list for verification.
For the input list [18, 6, 10, 3]
, the output is:
18 -> 6 -> 6 -> 2 -> 10 -> 1 -> 3
Discussed in https://github.com/mah-shamim/leet-code-in-php/discussions/515