shlogg · Early preview
Md Ariful Haque @mah-shamim

Insert GCD Nodes In Linked List With PHP Solution

We need to insert nodes between every pair of adjacent nodes in a linked list with values equal to the greatest common divisor (GCD) of them. We'll traverse the list, calculate GCD for each pair and insert new nodes accordingly.

2807. Insert Greatest Common Divisors in Linked List
Difficulty: Medium
Topics: Linked List, Math, Number Theory
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 d...