[go: up one dir, main page]

0% found this document useful (0 votes)
15 views5 pages

Add Two Number

The document provides a Java solution for the LeetCode problem 'Add Two Numbers', which involves adding two non-empty linked lists representing non-negative integers stored in reverse order. It includes the Java code implementation, example input and output, and a step-by-step dry run of the algorithm. Additionally, a modified version using integer arrays instead of linked lists is presented, along with its corresponding code and output.

Uploaded by

Akash Satdeve
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views5 pages

Add Two Number

The document provides a Java solution for the LeetCode problem 'Add Two Numbers', which involves adding two non-empty linked lists representing non-negative integers stored in reverse order. It includes the Java code implementation, example input and output, and a step-by-step dry run of the algorithm. Additionally, a modified version using integer arrays instead of linked lists is presented, along with its corresponding code and output.

Uploaded by

Akash Satdeve
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

Here’s the Java solution for the LeetCode problem Add Two

Numbers with:

 Code
 Example Input and Output
 Dry Run (Step-by-Step Explanation)

✅ Problem Summary:
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.

✅ Java Code:
java
CopyEdit
class ListNode {
int val;
ListNode next;
ListNode() {}
ListNode(int val) { this.val = val; }
ListNode(int val, ListNode next) { this.val = val;
this.next = next; }
}

public class AddTwoNumbers {


public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
ListNode dummyHead = new ListNode(0); // dummy node
ListNode current = dummyHead;
int carry = 0;

while (l1 != null || l2 != null || carry != 0) {


int sum = carry;

if (l1 != null) {
sum += l1.val;
l1 = l1.next;
}

if (l2 != null) {
sum += l2.val;
l2 = l2.next;
}

carry = sum / 10;


current.next = new ListNode(sum % 10);
current = current.next;
}

return dummyHead.next;
}
}

✅ Example Input:
Let's assume:

 l1 = [2 -> 4 -> 3] (Represents 342)


 l2 = [5 -> 6 -> 4] (Represents 465)

Expected Output:
rust
CopyEdit
[7 -> 0 -> 8] → 807 (reverse of 342 + 465 = 807)

✅ Dry Run (Step-by-step):


makefile
CopyEdit
Carry = 0
Result = dummy -> []

Step 1:
l1.val = 2, l2.val = 5, carry = 0
Sum = 2 + 5 + 0 = 7
carry = 0
Create node with 7
Result = [7]

Step 2:
l1.val = 4, l2.val = 6, carry = 0
Sum = 4 + 6 + 0 = 10
carry = 1
Create node with 0
Result = [7 -> 0]

Step 3:
l1.val = 3, l2.val = 4, carry = 1
Sum = 3 + 4 + 1 = 8
carry = 0
Create node with 8
Result = [7 -> 0 -> 8]

Final carry = 0, so stop.

Output: [7 -> 0 -> 8]

✅ Output:
java
CopyEdit
7 -> 0 -> 8

Which represents 807.

Would you like me to give a full runnable main method that


builds these inputs too?
You said:

without listnode
ChatGPT said:

Got it! You want the Add Two Numbers solution in Java without
using the ListNode class, i.e., using arrays or similar structure —
for simplicity and clarity.

Here’s a version that assumes you're working with two integer


arrays in reverse order instead of linked lists.

✅ Problem (Modified Version):

Add two numbers represented as arrays in reverse order, and


return the result as an array (also in reverse order).
Example:

 arr1 = {2, 4, 3} → represents 342


 arr2 = {5, 6, 4} → represents 465
 Output: {7, 0, 8} → represents 807

✅ Java Code (Without ListNode):


java
CopyEdit
import java.util.*;

public class AddTwoNumbersArray {


public static int[] addTwoNumbers(int[] arr1, int[] arr2)
{
List<Integer> result = new ArrayList<>();
int i = 0, carry = 0;

while (i < arr1.length || i < arr2.length || carry !=


0) {
int sum = carry;

if (i < arr1.length) {
sum += arr1[i];
}
if (i < arr2.length) {
sum += arr2[i];
}

result.add(sum % 10);
carry = sum / 10;
i++;
}

// Convert list to array


int[] ans = new int[result.size()];
for (int j = 0; j < result.size(); j++) {
ans[j] = result.get(j);
}

return ans;
}

public static void main(String[] args) {


int[] arr1 = {2, 4, 3}; // Represents 342
int[] arr2 = {5, 6, 4}; // Represents 465
int[] sum = addTwoNumbers(arr1, arr2);

System.out.print("Result: ");
for (int num : sum) {
System.out.print(num + " ");
}
}
}

✅ Output:
makefile
CopyEdit
Result: 7 0 8

✅ Dry Run:
makefile
CopyEdit
arr1 = [2, 4, 3]
arr2 = [5, 6, 4]

i = 0 → 2 + 5 + 0 = 7 → carry = 0 → result = [7]


i = 1 → 4 + 6 + 0 = 10 → carry = 1 → result = [7, 0]
i = 2 → 3 + 4 + 1 = 8 → carry = 0 → result = [7, 0, 8]

Final output = [7, 0, 8]

You might also like