Java Linked List Program · OT44
Add Two Numbers Stored as Linked Lists in Java
With the least significant digit first, the lists are already in the order you add numbers on paper: digit by digit, carrying into the next position.
Problem Statement
Two non-negative integers are stored as linked lists, one digit per node, with the least significant digit first. Return their sum as a new list in the same format.
The lists may have different lengths, and the sum may have one more digit than the longer input.
Input/Output Examples
9→9 + 1Output0→0→1Explanation: 99 + 1 = 100; the last carry creates a new node.3→4→2 + 4→6→5Output7→0→8Explanation: 243 + 564 = 807.0 + 0Output0Explanation: a single zero digit.Visual Explanation
- Digit 1sum3 + 4 = 7carry 0
- Digit 2sum4 + 6 = 10write 0, carry 1
- Digit 3sum2 + 5 + 1 = 8carry 0
- Done243 + 564 = 8077→0→8
The loop continues while either list has digits or a carry remains, which handles different lengths and a final carry with the same code.
Approaches
Both approaches add digit by digit. One loops with a dummy head; the other recurses with the carry as a parameter.
Approach 1: Iterative With Carry (Recommended)
Idea: walk both lists together, adding the digits and the carry, and append sum % 10 to the result.
- Create a dummy node and set
carry = 0. - While either list has nodes or
carryis not 0, add the available digits and the carry. - Append
sum % 10and setcarry = sum / 10. - Return
dummy.next.
Time: O(max(n, m)) · Space: O(1). Excluding the result list.
Approach 2: Recursion
Idea: each call creates one result digit and recurses on the next nodes with the new carry.
- Stop when both lists are used up and there is no carry.
- Create a node for
sum % 10. - Set its
nextto the recursive result withsum / 10as the carry.
Time: O(max(n, m)) · Space: O(max(n, m)). Recursion depth.
Java Solutions
Each tab is a complete program that runs on Java 17 or later. Both programs print the same output for the same inputs.
Approach 1: Iterative With Carry (Recommended)
The single loop condition covers unequal lengths and the final carry.
public class AddListsIterative {
static class Node {
int val;
Node next;
Node(int val) { this.val = val; }
}
static Node of(int... values) {
Node dummy = new Node(0);
Node t = dummy;
for (int v : values) {
t = t.next = new Node(v);
}
return dummy.next;
}
static String show(Node n) {
StringBuilder sb = new StringBuilder();
for (; n != null; n = n.next) {
sb.append(n.val).append(n.next != null ? "->" : "");
}
return sb.toString();
}
// Digits are stored least significant first, so add from the heads like on paper
static Node add(Node a, Node b) {
Node dummy = new Node(0);
Node tail = dummy;
int carry = 0;
while (a != null || b != null || carry != 0) {
int sum = carry;
if (a != null) {
sum += a.val;
a = a.next;
}
if (b != null) {
sum += b.val;
b = b.next;
}
tail = tail.next = new Node(sum % 10);
carry = sum / 10;
}
return dummy.next;
}
public static void main(String[] args) {
System.out.println("9->9 + 1 = " + show(add(of(9, 9), of(1))) + " (99 + 1 = 100)");
System.out.println("3->4->2 + 4->6->5 = " + show(add(of(3, 4, 2), of(4, 6, 5))) + " (243 + 564 = 807)");
System.out.println("0 + 0 = " + show(add(of(0), of(0))));
}
}
9->9 + 1 = 0->0->1 (99 + 1 = 100) 3->4->2 + 4->6->5 = 7->0->8 (243 + 564 = 807) 0 + 0 = 0
Time complexity: O(max(n, m)) · Space complexity: O(1).
Approach 2: Recursion
Very short, but the call stack grows with the number of digits.
public class AddListsRecursive {
static class Node {
int val;
Node next;
Node(int val) { this.val = val; }
}
static Node of(int... values) {
Node dummy = new Node(0);
Node t = dummy;
for (int v : values) {
t = t.next = new Node(v);
}
return dummy.next;
}
static String show(Node n) {
StringBuilder sb = new StringBuilder();
for (; n != null; n = n.next) {
sb.append(n.val).append(n.next != null ? "->" : "");
}
return sb.toString();
}
// add(a, b, carry) builds one digit and recurses on the rest
static Node add(Node a, Node b) {
return add(a, b, 0);
}
private static Node add(Node a, Node b, int carry) {
if (a == null && b == null && carry == 0) {
return null;
}
int sum = carry + (a != null ? a.val : 0) + (b != null ? b.val : 0);
Node node = new Node(sum % 10);
node.next = add(a != null ? a.next : null, b != null ? b.next : null, sum / 10);
return node;
}
public static void main(String[] args) {
System.out.println("9->9 + 1 = " + show(add(of(9, 9), of(1))) + " (99 + 1 = 100)");
System.out.println("3->4->2 + 4->6->5 = " + show(add(of(3, 4, 2), of(4, 6, 5))) + " (243 + 564 = 807)");
System.out.println("0 + 0 = " + show(add(of(0), of(0))));
}
}
9->9 + 1 = 0->0->1 (99 + 1 = 100) 3->4->2 + 4->6->5 = 7->0->8 (243 + 564 = 807) 0 + 0 = 0
Time complexity: O(max(n, m)) · Space complexity: O(max(n, m)).
Complexity Analysis
| Approach | Time Complexity | Space Complexity |
|---|---|---|
| Iterative With Carry | O(max(n, m)) | O(1) |
| Recursion | O(max(n, m)) | O(max(n, m)) |
Here n and m are the list lengths. Each digit position is processed once; the result has at most one more digit than the longer list.
Interview Tips and Recommended Approach
- Keep looping while there is a carry. Forgetting it turns 99 + 1 into 00.
- Do not convert to integers. Lists can hold numbers far larger than
long. - Use a dummy head to avoid special-casing the first digit.
- Know the forward-order variant. With the most significant digit first, reverse the lists or use stacks.
Recommended Approach: Iterative With Carry
Why: it mirrors paper addition, handles any length and needs only one loop and a carry variable.