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:

 

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:
56712
+   6359
———–
63071

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

Algorithm:

  • 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:

When you run above program , you will get following output:
Please go through Frequently asked Interview programs in java  for more such programs
.

Was this post helpful?

Leave a Reply

Your email address will not be published. Required fields are marked *