# Add two numbers(reverse order) link list Problem - Leet Code Solution

July 23, 2019

## Problem Statement

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 contain a single digit. Add the two numbers and return it as a linked list.

You may assume the two numbers do not contain any leading zero, except the number 0 itself.

``````Example:

Input: (2 -> 4 -> 3) + (5 -> 6 -> 4)
Output: 7 -> 0 -> 8
Explanation: 342 + 465 = 807.``````

## Solution

``````private static ListNode addTwoNumbersHelper(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; }

if (sum > 9) {
sum = sum - 10;
carry = 1;
}
else {
carry = 0;
}

ListNode newNode = new ListNode(sum);
newNode.next = addTwoNumbersHelper(l1 != null ? l1.next : null, l2 != null ? l2.next : null, carry);
return newNode;
}
public static ListNode addTwoNumbers(ListNode l1, ListNode l2) {
}``````

## Explanation

This problem is rather simple. Since, the number is represented as link list in reverse order. i.e. the first node represent the last digit of a number. So, you can just started adding number as you traverse the two link list.

• take sum of two nodes, and carry if previously set.
• If the sum is more than single digit, set the carry.
• Now, new sum is our new node.
• Since, it will be last digit in the result number, and will be first node in link list.
• Its next pointer will be the one which will come out in next round of sum.
• So, we simply set its next and call same method in recursive manner. Which will again goes in recursive mode untill all link list is exhausted.
• And, we get our link list as sum.

## Similar Posts

### Remove nth Node from Last - Leet Code Solution

Problem Statement Given a linked list, remove the n-th node from the end of list…

### Index for Coding Problems

Sorting Problems Merge Sort Quick Sort Heap Sort Bubble Sort Selection Sort…

### Four Sum - Leet Code Solution

Problem Statement Given an array nums of n integers and an integer target, are…

System design interview is pretty common these days, specially if you are having…

### Longest Substring without repeating characters - Leet Code Solution

Problem Statement Given a string, find the length of the longest substring…

### Maximum Subarray Problem

Problem Statement You are given an array of integers. And, you have find the…

## Latest Posts

### Jenkins Pipeline with Jenkinsfile - How To Schedule Job on Cron and Not on Code Commit

Introduction In this post we will see following: How to schedule a job on cron…

### How to Git Clone Another Repository from Jenkin Pipeline in Jenkinsfile

Introduction There are some cases, where I need another git repository while…

### How to Fetch Multiple Credentials and Expose them in Environment using Jenkinsfile pipeline

Introduction In this post, we will see how to fetch multiple credentials and…

### Jenkins Pipeline - How to run Automation on Different Environment (Dev/Stage/Prod), with Credentials

Introduction I have an automation script, that I want to run on different…

### Jenkinsfile - How to Create UI Form Text fields, Drop-down and Run for Different Conditions

Introduction I had to write a CICD system for one of our project. I had to…

### Java Log4j Logger - Programmatically Initialize JSON logger with customized keys in json logs

Introduction Java log4j has many ways to initialize and append the desired…