Tap here to switch tabs
Problem
Submissions

Problem: Insert Greatest Common Divisors in Linked List

med
30 min
Try to solve the Insert Greatest Common Divisors in Linked List problem.

Statement

Given the head of a singly linked list where each node contains an integer value, insert a new node between every pair of adjacent nodes. The value of each inserted node must be equal to the greatest common divisor of the values of the two adjacent nodes.

Return the head of the linked list after inserting all the new nodes.

Note: The greatest common divisor of two integers is the largest positive integer that evenly divides both numbers.

Constraints:

  • The number of nodes in the list is in the range [1,5000][1, 5000].

  • 1≤1 \leq Node.val ≤1000\leq 1000

⋮
Tap here to switch tabs
Problem
Submissions

Problem: Insert Greatest Common Divisors in Linked List

med
30 min
Try to solve the Insert Greatest Common Divisors in Linked List problem.

Statement

Given the head of a singly linked list where each node contains an integer value, insert a new node between every pair of adjacent nodes. The value of each inserted node must be equal to the greatest common divisor of the values of the two adjacent nodes.

Return the head of the linked list after inserting all the new nodes.

Note: The greatest common divisor of two integers is the largest positive integer that evenly divides both numbers.

Constraints:

  • The number of nodes in the list is in the range [1,5000][1, 5000].

  • 1≤1 \leq Node.val ≤1000\leq 1000