You are given head, the first node of a singly linked list. Each node stores a positive integer, and the list is ordered from the head node toward the final node.
For every pair of adjacent nodes, calculate their greatest common divisor (GCD): the largest positive integer that divides both node values. Insert a new node containing that GCD between the two adjacent nodes.
Keep every original node in its original order, and return the head of the modified list. If head is null, return null.
Example 1:
Input: head = [14,25,9]
Output: [14,1,25,1,9]
Explanation: The GCDs of the adjacent pairs are 1 and 1, so those values are inserted between the original nodes.
Example 2:
Input: head = [18,30]
Output: [18,6,30]
Explanation: The only adjacent pair has GCD 6, which is placed between the two nodes.
Example 3:
Input: head = [8,12,20]
Output: [8,4,12,4,20]
Explanation: The adjacent GCDs are gcd(8,12) = 4 and gcd(12,20) = 4.
Constraints:
0 <= number of nodes in head <= 10^51 <= value in each node <= 1000head is either null or the first node of a singly linked listEach node has at most one next nodeNeed help or want more practice?