Skip to main content

Command Palette

Search for a command to run...

Solving Leetcode Interviews in Seconds with AI: Add Two Numbers II

Updated
3 min read

Introduction

In this blog post, we will explore how to solve the LeetCode problem "445" 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 two non-empty linked lists representing two non-negative integers. The most significant digit comes first and each of their nodes contains a single digit. Add the two numbers and return the sum as a linked list. You may assume the two numbers do not contain any leading zero, except the number 0 itself. Example 1: Input: l1 = [7,2,4,3], l2 = [5,6,4] Output: [7,8,0,7] Example 2: Input: l1 = [2,4,3], l2 = [5,6,4] Output: [8,0,7] Example 3: Input: l1 = [0], l2 = [0] Output: [0] Constraints: The number of nodes in each linked list is in the range [1, 100]. 0 <= Node.val <= 9 It is guaranteed that the list represents a number that does not have leading zeros. Follow up: Could you solve it without reversing the input lists?

Explanation

Here's the solution to add two numbers represented by linked lists (most significant digit first) without reversing the lists:

  • Utilize Stacks: Push the values from both linked lists onto separate stacks. This effectively reverses the order of digits, allowing addition from the least significant digit.
  • Simulate Addition: Pop values from the stacks and perform digit-by-digit addition, carrying over any excess to the next higher digit.
  • Construct the Result: Create a new linked list to store the sum, prepending new nodes to the list as digits are calculated, building the result from the most significant digit down.

  • Runtime Complexity: O(m + n), where m and n are the lengths of the linked lists.

  • Storage Complexity: O(m + n), primarily due to the stacks.

Code

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

def addTwoNumbers(l1: ListNode, l2: ListNode) -> ListNode:
    stack1 = []
    stack2 = []

    # Push the values from l1 onto stack1
    curr = l1
    while curr:
        stack1.append(curr.val)
        curr = curr.next

    # Push the values from l2 onto stack2
    curr = l2
    while curr:
        stack2.append(curr.val)
        curr = curr.next

    carry = 0
    result = None  # Head of the result linked list

    # Iterate while there are still digits in either stack or a carry
    while stack1 or stack2 or carry:
        val1 = stack1.pop() if stack1 else 0
        val2 = stack2.pop() if stack2 else 0

        sum_digits = val1 + val2 + carry
        carry = sum_digits // 10  # Calculate carry
        digit = sum_digits % 10   # Calculate the digit to append

        # Create a new node with the digit and prepend it to the result list
        new_node = ListNode(digit)
        new_node.next = result
        result = new_node

    return result

More from this blog

C

Chatmagic blog

2894 posts

Solving Leetcode Interviews in Seconds with AI: Add Two Numbers II