Addition, But Make It Linked 🔗➕

Adding Two Numbers – One Node at a Time

We’ve all done basic addition, right? But what happens when each digit is stored in a linked list and… in reverse order? Welcome to a fan-favorite problem: Add Two Numbers (LeetCode #2).

Let’s dive into the different ways to tackle it in Java – from the tried-and-true iterative method to a cheeky BigInteger hack.

The best data structure for solving it (and why!)

The obvious MVP here? Singly Linked Lists. Since the digits are stored in reverse order (1’s place first), we can iterate from the head without reversing anything manually. We just need to simulate digit-by-digit addition — like we did in school, carry and all.

And for tracking the result? A dummy head node makes things a breeze. It helps keep the logic simple while chaining together the resulting nodes.

Different approaches – brute force to optimized solutions

1: Iterative (Most Common)

class ListNode { int val; ListNode next; ListNode(int x) { val = x; } } public 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 sum = carry; if (l1 != null) { sum += l1.val; l1 = l1.next; } if (l2 != null) { sum += l2.val; l2 = l2.next; } carry = sum / 10; curr.next = new ListNode(sum % 10); curr = curr.next; } return dummy.next; }

2: Recursive

class ListNode { int val; ListNode next; ListNode(int x) { val = x; } } public class Solution { public ListNode addTwoNumbers(ListNode l1, ListNode l2) { return helper(l1, l2, 0); } private ListNode helper(ListNode l1, ListNode l2, int carry) { if (l1 == null && l2 == null && carry == 0) return null; int sum = carry; if (l1 != null) sum += l1.val; if (l2 != null) sum += l2.val; ListNode node = new ListNode(sum % 10); node.next = helper( l1 != null ? l1.next : null, l2 != null ? l2.next : null, sum / 10 ); return node; } }

3: BigInteger (Cheeky but Fast for Quick Prototypes)

import java.math.BigInteger; class ListNode { int val; ListNode next; ListNode(int x) { val = x; } } public class Solution { public ListNode addTwoNumbers(ListNode l1, ListNode l2) { BigInteger num1 = listToBigInteger(l1); BigInteger num2 = listToBigInteger(l2); BigInteger sum = num1.add(num2); return bigIntegerToList(sum); } private BigInteger listToBigInteger(ListNode node) { StringBuilder sb = new StringBuilder(); while (node != null) { sb.insert(0, node.val); // reverse order node = node.next; } return new BigInteger(sb.toString()); } private ListNode bigIntegerToList(BigInteger num) { String str = new StringBuilder(num.toString()).reverse().toString(); ListNode dummy = new ListNode(0); ListNode curr = dummy; for (char c : str.toCharArray()) { curr.next = new ListNode(c - '0'); curr = curr.next; } return dummy.next; } }

⚠️Common mistakes & pitfalls to avoid

  • Forgetting the final carry: After the loop ends, don’t forget to check if carry != 0. It needs to be added as a new node.

  • Mixing up digit order: The problem assumes the digits are in reverse order. Make sure not to reverse them unless specifically required.

  • Recursive stack overflow: For very long lists, recursion might blow the stack. The iterative version is safer for large inputs.

📊Time & Space complexity analysis

Iterative and Recursive Approaches:

  • Time: O(max(m, n))
    We go through each node once from both lists.

  • Space: O(max(m, n))
    One node per digit in the result, plus recursion stack (if recursive).

BigInteger Approach:

  • Time: O(n + m), but depends on internal BigInteger operations

  • Space: O(n + m) for string building and parsing

🌍Real-world applications

  • Embedded systems dealing with long numbers that can’t fit in standard data types.

  • Blockchain wallets where arithmetic on long digit sequences is common.

  • Custom calculators or compilers for user-defined numeric types.

  • And of course... your friendly neighborhood linked list interview questions


I hope you found this article insightful! Keep exploring, keep learning, and don’t forget to check back daily for more exciting problem breakdowns. If you know someone who would benefit from this, share it with them and help them grow too! That’s it for today—see you in the next post!

                                                                                                                                         Signing off!! 

                                                                                                Master DSA One Problem at a Time :)

Comments

Popular posts from this blog

Trailing Zeros in Factorial – How Many and Why?

Divisible Drama — Who’s In, Who’s Out?

Zero Array After Queries