Problem Summary
You are given two non-empty singly linked lists that represent two non-negative integers.
Each linked list stores the digits in reverse order.
Each node contains one digit from 0–9.
Your task is to add the two numbers and return the sum as a linked list in the same reverse order format.
Example
l1 = 2 → 4 → 3
l2 = 5 → 6 → 4
Output = 7 → 0 → 8
Because 342 + 465 = 807.
Constraints
The lists can be of different lengths.
Each node contains a single digit.
You must handle carries properly.
The number has no leading zeros, except when the number is zero itself.
Approach in Simple English
This problem is basically performing addition the same way we do on paper, but digit by digit from right to left.
Since the digits are stored in reverse order inside the linked lists, the rightmost digit is already the first node.
So we can add digits from both lists starting from the head.
Step-by-step understanding:
- Start with pointers for both linked lists.
If a list has finished, treat its digit as zero. - At every position, calculate
digitSum = digitFromList1 + digitFromList2 + carry - The output digit will be digitSum % 10
The new carry will be digitSum / 10 (integer division) - Create a new node with the output digit and attach it to the result list.
- Move the pointers forward in both lists.
- After the loop ends, if carry > 0, add one extra node for it.
The easiest way to build the output list is to use a dummy head node so you don’t worry about special cases for the first node.
Time complexity: O(max(n, m))
Space complexity: O(max(n, m)) for result list, O(1) extra space.
Java Code (Iterative Solution)
// Definition for singly
You said:
Ignore my formatting instructions. Use whatever is best for tutorial.
ChatGPT said:
Great — here is a clean, polished, professional Java tutorial for LeetCode Problem 2: Add Two Numbers, using the most suitable formatting for clarity and readability.
LeetCode 2: Add Two Numbers (Java Tutorial)
Problem Summary
You are given two non-empty linked lists representing two non-negative integers.
The digits are stored in reverse order, and each node contains a single digit.
Your task is to add the two numbers and return the result as a linked list, also in reverse order.
Example:
Input: (2 -> 4 -> 3) + (5 -> 6 -> 4) Output: 7 -> 0 -> 8 Explanation: 342 + 465 = 807
Digits are stored in reverse, so the head represents the ones place.
Approach
Think about how you add two numbers on paper from right to left:
342 + 465 ------ 807
You add digits from the last place first and carry over if needed.
The linked lists already store digits from right to left, so:
- First nodes → ones place
- Next nodes → tens place
- Next nodes → hundreds place
So we can simply walk through both lists and add corresponding digits.
Steps
- Initialize:
carry = 0- A dummy node to build the result list easily
- A pointer
currentto the dummy node
- Loop while either list still has nodes OR
carry > 0. - Get the values of the current nodes in both lists.
If a list is finished, use0. - Calculate:
sum = x + y + carry digit = sum % 10 carry = sum / 10 - Create a new node with
digitand attach it to the result. - Move to the next nodes in both lists.
- After the loop, return
dummy.next.
This avoids any special-case code and handles different lengths automatically.
Java Code (Clean and Simple)
// Definition for singly-linked list.
public class ListNode {
int val;
ListNode next;
ListNode(int x) { val = x; }
}
class Solution {
public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
ListNode dummy = new ListNode(0);
ListNode curr = dummy;
int carry = 0;
while (l1 != null || l2 != null || carry != 0) {
int x = (l1 != null) ? l1.val : 0;
int y = (l2 != null) ? l2.val : 0;
int sum = x + y + carry;
carry = sum / 10;
curr.next = new ListNode(sum % 10);
curr = curr.next;
if (l1 != null) l1 = l1.next;
if (l2 != null) l2 = l2.next;
}
return dummy.next;
}
}
Dry Run (Step-by-Step)
Let’s dry-run the input:
l1 = 2 -> 4 -> 3 (represents 342) l2 = 5 -> 6 -> 4 (represents 465)
Initial State
carry = 0 result = empty
Step 1
x = 2, y = 5 sum = 2 + 5 + 0 = 7 digit = 7, carry = 0 result = 7
Step 2
x = 4, y = 6 sum = 4 + 6 = 10 digit = 0, carry = 1 result = 7 -> 0
Step 3
x = 3, y = 4 sum = 3 + 4 + 1 = 8 digit = 8, carry = 0 result = 7 -> 0 -> 8
Loop ends
Both lists ended and carry = 0.
Final output:
7 -> 0 -> 8
