Skip to main content

Command Palette

Search for a command to run...

Solving Leetcode Interviews in Seconds with AI: Double a Number Represented as a Linked List

Updated
3 min read

Introduction

In this blog post, we will explore how to solve the LeetCode problem "2816" 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

You are given the head of a non-empty linked list representing a non-negative integer without leading zeroes. Return the head of the linked list after doubling it. Example 1: Input: head = [1,8,9] Output: [3,7,8] Explanation: The figure above corresponds to the given linked list which represents the number 189. Hence, the returned linked list represents the number 189 2 = 378. Example 2: Input: head = [9,9,9] Output: [1,9,9,8] Explanation: The figure above corresponds to the given linked list which represents the number 999. Hence, the returned linked list reprersents the number 999 2 = 1998. Constraints: The number of nodes in the list is in the range [1, 104] 0 <= Node.val <= 9 The input is generated such that the list represents a number that does not have leading zeros, except the number 0 itself.

Explanation

Here's a breakdown of the approach and the Python code:

  • Convert to Number, Double, Convert Back: The most straightforward approach involves converting the linked list to an integer, doubling the integer, and then converting the doubled integer back into a linked list.

  • In-place Doubling with Carry: A more efficient method doubles the linked list in place, simulating the multiplication process digit by digit with carry propagation. We traverse the list from right to left, doubling each digit and handling carries appropriately.

  • Handle Leading Carry: If a carry remains after processing all digits, we add a new node with the carry value at the beginning of the linked list.

  • Runtime & Storage Complexity: O(N) runtime complexity, O(1) storage complexity. (where N is the number of nodes in the linked list)

Code

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

def double_a_linked_list(head):
    """
    Doubles a linked list representing a non-negative integer.

    Args:
        head: The head of the linked list.

    Returns:
        The head of the doubled linked list.
    """

    # Helper function to reverse the linked list
    def reverse_linked_list(head):
        prev = None
        curr = head
        while curr:
            next_node = curr.next
            curr.next = prev
            prev = curr
            curr = next_node
        return prev

    # Reverse the linked list to process it from right to left (least significant digit first)
    head = reverse_linked_list(head)

    curr = head
    carry = 0
    while curr:
        doubled_val = curr.val * 2 + carry
        curr.val = doubled_val % 10
        carry = doubled_val // 10
        curr = curr.next

    # If there's a carry remaining after processing all digits, add a new node at the end
    if carry:
        head = reverse_linked_list(head)
        new_head = ListNode(carry)
        new_head.next = head
        return new_head

    # Reverse the linked list again to get it back in the original order
    head = reverse_linked_list(head)
    return head

More from this blog

C

Chatmagic blog

2894 posts