Add Two Numbers 🧠
LeetCode Link: Add Two Numbers
Difficulty: Medium
Problem Explanation 📝
Problem: LeetCode 2 - Add Two Numbers
Description: You are given two non-empty linked lists representing two non-negative integers. The digits are stored in reverse order, and each of their nodes contains a single digit. Add the two numbers and return the sum as a linked list.
Intuition: We can traverse the two linked lists simultaneously, adding the digits at the same position and keeping track of the carry. As we move forward, we create a new node for the sum and update the carry for the next iteration. If any linked list has more digits left, we continue the process until both lists are fully traversed.
Approach:
- Initialize a dummy node to the head of the result list.
- Initialize variables
carry
andsum
to 0. - Traverse both linked lists simultaneously until both are fully traversed.
- At each step, compute the sum of digits and the carry for the next iteration.
- Create a new node with the sum and attach it to the result list.
- Move the current pointers of both input lists to the next nodes.
- If any list has remaining digits, continue adding them to the result.
- Return the head of the result list after skipping the dummy node.
⌛ Time Complexity: The time complexity is O(max(N, M)), where N and M are the number of nodes in the input linked lists. We traverse both lists once.
💾 Space Complexity: The space complexity is O(max(N, M)), where N and M are the number of nodes in the input linked lists. The extra space is used to store the result list.
Solutions 💡
Cpp 💻
class Solution {
public:
ListNode *addTwoNumbers(ListNode *l1, ListNode *l2) {
ListNode *dummy = new ListNode(0);
ListNode *current = dummy;
int carry = 0;
while (l1 || l2) {
int sum = carry;
if (l1) {
sum += l1->val;
l1 = l1->next;
}
if (l2) {
sum += l2->val;
l2 = l2->next;
}
carry = sum / 10;
current->next = new ListNode(sum % 10);
current = current->next;
}
if (carry) {
current->next = new ListNode(carry);
}
return dummy->next;
}
};
Python 🐍
# Definition for singly-linked list.
# class ListNode:
# def __init__(self, val=0, next=None):
# self.val = val
# self.next = next
class Solution:
def addTwoNumbers(self, l1: ListNode, l2: ListNode) -> ListNode:
dummy = ListNode()
current = dummy
carry = 0
while l1 or l2:
val1 = l1.val if l1 else 0
val2 = l2.val if l2 else 0
total = val1 + val2 + carry
carry = total // 10
digit = total % 10
current.next = ListNode(digit)
current = current.next
if l1:
l1 = l1.next
if l2:
l2 = l2.next
if carry:
current.next = ListNode(carry)
return dummy.next