java.lang.Object
g2801_2900.s2807_insert_greatest_common_divisors_in_linked_list.Solution

public class Solution extends Object
2807 - Insert Greatest Common Divisors in Linked List.<p>Medium</p> <p>Given the head of a linked list <code>head</code>, in which each node contains an integer value.</p> <p>Between every pair of adjacent nodes, insert a new node with a value equal to the <strong>greatest common divisor</strong> of them.</p> <p>Return <em>the linked list after insertion</em>.</p> <p>The <strong>greatest common divisor</strong> of two numbers is the largest positive integer that evenly divides both numbers.</p> <p><strong>Example 1:</strong></p> <p><img src="https://assets.leetcode.com/uploads/2023/07/18/ex1_copy.png" alt="" /></p> <p><strong>Input:</strong> head = [18,6,10,3]</p> <p><strong>Output:</strong> [18,6,6,2,10,1,3]</p> <p><strong>Explanation:</strong> The 1<sup>st</sup> diagram denotes the initial linked list and the 2<sup>nd</sup> diagram denotes the linked list after inserting the new nodes (nodes in blue are the inserted nodes).</p> <ul> <li>We insert the greatest common divisor of 18 and 6 = 6 between the 1<sup>st</sup> and the 2<sup>nd</sup> nodes.</li> <li>We insert the greatest common divisor of 6 and 10 = 2 between the 2<sup>nd</sup> and the 3<sup>rd</sup> nodes.</li> <li>We insert the greatest common divisor of 10 and 3 = 1 between the 3<sup>rd</sup> and the 4<sup>th</sup> nodes.</li> </ul> <p>There are no more adjacent nodes, so we return the linked list.</p> <p><strong>Example 2:</strong></p> <p><img src="https://assets.leetcode.com/uploads/2023/07/18/ex2_copy1.png" alt="" /></p> <p><strong>Input:</strong> head = [7]</p> <p><strong>Output:</strong> [7]</p> <p><strong>Explanation:</strong> The 1<sup>st</sup> diagram denotes the initial linked list and the 2<sup>nd</sup> diagram denotes the linked list after inserting the new nodes. There are no pairs of adjacent nodes, so we return the initial linked list.</p> <p><strong>Constraints:</strong></p> <ul> <li>The number of nodes in the list is in the range <code>[1, 5000]</code>.</li> <li><code>1 <= Node.val <= 1000</code></li> </ul>
  • Constructor Details

    • Solution

      public Solution()
  • Method Details

    • insertGreatestCommonDivisors

      public ListNode insertGreatestCommonDivisors(ListNode head)