Skip to main content

Command Palette

Search for a command to run...

Solving Leetcode Interviews in Seconds with AI: Insert Greatest Common Divisors in Linked List

Updated
3 min read

Introduction

In this blog post, we will explore how to solve the LeetCode problem "2807" using AI. LeetCode is a popular platform for preparing for coding interviews, and with the help of AI tools like Chatmagic, we can generate solutions quickly and efficiently - helping you pass the interviews and get the job offer without having to study for months.

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. Example 1: Input: head = [18,6,10,3] Output: [18,6,6,2,10,1,3] Explanation: The 1st diagram denotes the initial linked list and the 2nd diagram denotes the linked list after inserting the new nodes (nodes in blue are the inserted nodes). - We insert the greatest common divisor of 18 and 6 = 6 between the 1st and the 2nd nodes. - We insert the greatest common divisor of 6 and 10 = 2 between the 2nd and the 3rd nodes. - We insert the greatest common divisor of 10 and 3 = 1 between the 3rd and the 4th nodes. There are no more adjacent nodes, so we return the linked list. Example 2: Input: head = [7] Output: [7] Explanation: The 1st diagram denotes the initial linked list and the 2nd diagram denotes the linked list after inserting the new nodes. There are no pairs of adjacent nodes, so we return the initial linked list. Constraints: The number of nodes in the list is in the range [1, 5000]. 1 <= Node.val <= 1000

Explanation

  • Iterate and Insert: Traverse the linked list, and for each pair of adjacent nodes, calculate their greatest common divisor (GCD).
    • Create New Node: Create a new node with the calculated GCD as its value.
    • Insert into List: Insert the new node between the two adjacent nodes in the linked list.
  • Time Complexity: O(N log(M)), where N is the number of nodes in the linked list, and M is the maximum value of a node. This is due to iterating through the list (O(N)) and calculating the GCD for each pair (O(log(M))). *Space Complexity: O(1), as we are only using a constant amount of extra space.

Code

    class ListNode:
    def __init__(self, val=0, next=None):
        self.val = val
        self.next = next

class Solution:
    def insertGreatestCommonDivisors(self, head: ListNode) -> ListNode:
        def gcd(a, b):
            if b == 0:
                return a
            return gcd(b, a % b)

        curr = head
        while curr and curr.next:
            new_node = ListNode(gcd(curr.val, curr.next.val))
            new_node.next = curr.next
            curr.next = new_node
            curr = new_node.next
        return head

More from this blog

C

Chatmagic blog

2894 posts