Member-only story

Add Two Numbers — Leetcode 2

Java Solution for Add two numbers problem

Suraj Mishra
3 min readNov 8, 2022

Originally Published in https://asyncq.com/

Introduction

  • In this article, we will solve Leetcode problem #2 which is good to learn and operate on Linked List.

Problem Statement

  • We have been given two non-empty LinkedList that represents non-negative integers.
  • Digits are stored in reverse order and doesn't contain any leading zeros.
  • We need to add these two numbers and return the result as LinkedList.

Examples

  • In below example we can are adding 2+5 = 7, 4+6 = 10, 3+4+1(carry) = 8
    hence we need to return the result as [7,0,8]

Solutions

Intuition

  • The institution is simple that we will traverse two LinkedList simultaneously.
  • If both the list are null then we don’t have anything to add so we can break out of the loop.
  • But if one of them is null then we can safely assume 0 as the value and add that to…

--

--

Suraj Mishra
Suraj Mishra

Written by Suraj Mishra

Staff Software Engineer @PayPal ( All opinions are my own and not of my employer )

No responses yet