Insert GCD Nodes in a Linked List
Checking your account…
Sign in to save your code and progress across devices. The lesson and problem statement remain public.
Loading the interactive Interview workspace.If it does not appear, the problem and learning material remain readable, but browser execution is unavailable.Reload Interview workspace
Problem
Implement Solution.insertGreatestCommonDivisors(head). Between every pair of original adjacent nodes, insert a node containing their greatest common divisor and return head.
Starter code
class Solution:
def insertGreatestCommonDivisors(self, head):
passTest cases
sample
{
"args": [
{
"$type": "linked-list",
"values": [
18,
6,
10,
3
]
}
]
}Expected: {"$type":"linked-list","values":[18,6,6,2,10,1,3]}
Wizard outline
- Step 1: Initialize Solution.insertGreatestCommonDivisors
Replace the empty starter with the first real state owned by Solution.insertGreatestCommonDivisors. A small, named state is easier to verify than a complete algorithm. Establish it before adding the branch or loop that changes it.
- Step 2: Assemble the primary transition
Extend the initialized state with the next contiguous part of the popular solution. The transition explains how one input element or operation changes the state; boundaries are easier to reason about after this invariant is visible.
- Step 3: Pass the One Node case
Complete the readable core algorithm for one representative Interview case. Save the next original node before inserting so generated nodes are not processed again.
- Step 4: Harden the Sample boundary
Repair the reviewed boundary and pass the complete submission contract. Each iteration processes exactly one original adjacent pair and inserts its gcd between them without changing original order. Advancing to the saved neighbor ensures every and only original pair is processed.
Footguns and prerequisites
- Advance to the saved original neighbor, not to the newly inserted node.
- linked lists
- operators and expressions
Reviewed references
Practice prerequisites
- Relink Nodes Safely(opens in a new tab)
Relink Nodes Safely isolates every untouched node keeps its original next link, and at most one predecessor changes to the removed node’s former successor. That focused state discipline is required when implementing insert gcd linked list as a complete Interview Problem.
Recommended approach and implementation
While current has an original next node, save it, insert gcd(current.val,next.val), link the inserted node to next, then advance current to next.
Why it works: Each iteration processes exactly one original adjacent pair and inserts its gcd between them without changing original order. Advancing to the saved neighbor ensures every and only original pair is processed.
class Solution:
def insertGreatestCommonDivisors(self, head):
"""
Checkpoint 1: initialize the state owned by this Interview contract.
Checkpoint 2: assemble the primary transition without hiding the boundary.
"""
from math import gcd
current = head
while current and current.next:
following = current.next
current.next = ListNode(gcd(current.val, following.val), following)
current = following
return head