Skip to content

Practice Insert Greatest Common Divisors in Linked List

Read the task, work through the examples, and check your approach against the constraints. Use the original LeetCode task for coding and submissions.

Medium#2807
LeetCode Problem

Insert Greatest Common Divisors in Linked List

Problem Statement

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.

Source: Insert Greatest Common Divisors in Linked List on LeetCode. Use the original task for its coding environment and submissions.

Examples

# Example 1

Input
head = [18,6,10,3]
Output
[18,6,6,2,10,1,3]

# Example 2

Input
head = [7]
Output
[7]

Constraints

The number of nodes in the list is in the range [1, 5000].
1 <= Node.val <= 1000

Related LeetCode Problems

Practice more problems similar to Insert Greatest Common Divisors in Linked List.

Review your practice with PhantomCodeAI

After trying the problem yourself, use the app to discuss your approach, complexity, and edge cases.