Add two numbers represented by Linked List in java

If you want to practice data structure and algorithm programs, you can go through data structure and algorithm interview questions.

Java Linked List Interview Programs:

    How to reverse a linked list in java How to reverse a linked list in pairs in java How to find middle element of linked list in java How to detect a loop in linked list in java Find start node of loop in linkedlist How to find nth element from end of linked list How to check if linked list is palindrome in java Add two numbers represented by linked list in java
Given two number represent by linked list, calculate sum of the numbers and store result in new linked list. Each node of linked list is represented by single digit and head node is most significant digit.
For example:
Sum of two number:
+   6359

So it will be represented in below format as linked list:


  • Create two linkedlist which will represent above two numbers.
  • Reverse both linked list.
  • Add two node values (Each node is being represented as single digit)  starting from heads of two linkedlist.
  • If sum is of above two node values is more than 10, then forward the carry.
  • Follow basic mathematical rules for addition.
Below image will make it clear:

  • Reverse the result , so that we will get actual sum of numbers.

Java program:

package org.arpit.java2blog;

public class AddTwoNumberLinkedList{
  * @Author: Arpit Mandliya
  * */
 private static Node head;

 private static class Node {
  private int value;
  private Node next;

  Node(int value) {
   this.value = value;


 public void addToTheLast(Node node) {

  if (head == null) {
   head = node;
  } else {
   Node temp = head;
   while ( != null)
    temp =; = node;

 public void printList(Node printNode) {
  Node temp = printNode;
  while (temp != null) {
   System.out.format("%d ", temp.value);
   temp =;

 public static Node reverseLinkedList(Node node) {
      if (node == null || == null) {
          return node;

      Node remaining = reverseLinkedList(; = node; = null;
     return remaining;

 // This function will do sum of numbers represented by linked list
 public Node findSumOfNumbers(Node l1, Node l2) {
  int carry =0;
  Node newHead = null;
  Node tempNodeForIteration=null;
  int sum=0;

  int firstIter=0;
  while(l1!=null || l2!=null)


   // Check if it first node for the result
    tempNodeForIteration = new Node(sum);
    Node tempSumNode=new Node(sum);;;
   Node tempNode=new Node(carry);;
  return newHead;

 public static void main(String[] args) {
  AddTwoNumberLinkedList list = new AddTwoNumberLinkedList();
  // Creating a linked list
  Node head1=new Node(5);
  list.addToTheLast(new Node(6));
  list.addToTheLast(new Node(7));
  list.addToTheLast(new Node(1));
  list.addToTheLast(new Node(2));
  System.out.print("Number 1:  ");
  Node head2=new Node(6);
      list.addToTheLast(new Node(3));
  list.addToTheLast(new Node(5));
  list.addToTheLast(new Node(9));
  System.out.print("Number 2:  ");
  // Reversing first linkedList
  //Reversing second linkedList
  // function to find sum of two linkedlist represent by number
  Node result= list.findSumOfNumbers(head1,head2);
  // Reverse the above linkedlist to get actual sum
  System.out.print("Sum:  ");


When you run above program , you will get following output:
Number 1:  5 6 7 1 2 
Number 2:  6 3 5 9 
Sum:  6 3 0 7 1 
Please go through Frequently asked Interview programs in java  for more such programs.

Written by Arpit:

If you have read the post and liked it. Please connect with me on Facebook | Twitter | Google Plus


Java tutorial for beginners Copyright © 2012