Java Program to Add Two Numbers using Linked List

This is a Java Program to add two large numbers using Linked List. A linked list is a data structure consisting of a group of nodes which together represent a sequence. Under the simplest form, each node is composed of a data and a reference (in other words, a link) to the next node in the sequence. This structure allows for efficient insertion or removal of elements from any position in the sequence.
Maximum value that can be stored in the primitive datatypes ‘int’ and ‘long’ is 231 and 263 respectively. Hence values larger than this cannot be represented using int or long. An alternative is to use the BigInteger class which is available in java as java.math.BigInteger . However, here we will be applying concepts of linked lists to add very large numbers.

Here is the source code of the Java program to add two large numbers using Linked List. The Java program is successfully compiled and run on a Windows system. The program output is also shown below.

  1. /*
  2.  *  Java Program to add two large numbers using Linked List
  3.  */
  4.  
  5. import java.util.*;
  6.  
  7. public class AddLargeNumbersUsingLinkedLists
  8. {
  9.     public static void main(String[] args)
  10.     {    
  11.         Scanner scan = new Scanner(System.in);
  12.         /* Create Linked Lists */
  13.         LinkedList<Integer> num1 = new LinkedList<Integer>();
  14.         LinkedList<Integer> num2 = new LinkedList<Integer>();
  15.         LinkedList<Integer> ans = new LinkedList<Integer>();
  16.         /* Accept numbers */
  17.         System.out.println("Adding Large Numbers Using Linked Lists Test\n");
  18.         System.out.println("Enter number 1");
  19.         String s1 = scan.next();
  20.         System.out.println("Enter number 2");
  21.         String s2 = scan.next();
  22.         /* Store digits in lists */
  23.         int l1 = s1.length(), l2 = s2.length();        
  24.         for (int i = l1 - 1; i >= 0; i--)
  25.             num1.add(s1.charAt(i) - '0');
  26.         for (int i = l2 - 1; i >= 0; i--)
  27.             num2.add(s2.charAt(i) - '0');
  28.         /* Adding digits and storing in ans list */
  29.         int len = l1 > l2 ? l1 : l2;
  30.         int carry = 0;
  31.         for (int i = 0; i < len; i++)
  32.         {
  33.             int d1 = 0, d2 = 0;            
  34.             try {
  35.                 d1 = num1.get(i);
  36.             } 
  37.             catch(Exception e){}            
  38.             try {
  39.                 d2 = num2.get(i);
  40.             } 
  41.             catch(Exception e){}                        
  42.             int x = d1 + d2 + carry;
  43.             ans.add(x % 10);
  44.             carry = x / 10;
  45.         }
  46.         /* Adding carry */
  47.         while (carry != 0)
  48.         {
  49.             ans.add(carry % 10);
  50.             carry /= 10;
  51.         }
  52.         /* Printing ans list */
  53.         System.out.print("\nSum = ");
  54.         for (int i = ans.size() - 1; i >= 0; i--)
  55.             System.out.print(ans.get(i));
  56.         System.out.println();        
  57.     }
  58. }

Adding Large Numbers Using Linked Lists Test
 
Enter number 1
47573267684781773734658371972483657818736465
Enter number 2
1287486758723589773489812734285684586837498748865872358
 
Sum = 1287486758771163041174594508020342958809982406684608823
 
 
 
Adding Large Numbers Using Linked Lists Test
 
Enter number 1
54723648627465378475862384728763573645862736573563826875476567826384627357235724
87246375782638746723578634757823647236757658726345273864725762378623572365657236
573467236762365726846574658736453476582368991237
Enter number 2
23472356764817284635723642478356785728497172547386528348732572836483657582375823
65726481943756235876458237483764572836562387567823657823476417832685638756723658
491819284782843637856284719784
 
Sum = 54723648627465378499334741493580858281586379051920612603973740373771155705
96829770894741540876329089305116701579883113215896210109846701288149946447230189
133654406152875519089385338393943519297114438653711021
 
 
 
Adding Large Numbers Using Linked Lists Test
 
Enter number 1
85676347892358776738492384576783294876574923843567754398557493284767549328457543
92847567893284375382918356348293847564892347654839846589238457839382374653489283
75648392384756738298475674839234856743892984567849329123847548329138475489238465
Enter number 2
34637829234856543728928346578434576754894857657849876754838375674839387476574839
38756578438756783938756574839387657584932837567483928435743534567657267367346572
36576572687365347658235826357298347918239102391084345435648274982981029310849376
 
Sum = 12031417712721532046742073115521787163146978150141763115339586895960693680
50323833160414633204115932167493118768150514982518522232377502498199240703964202
08358561222496507212208595671150119653320466213208695893367455949582331211950480
0087841

Sanfoundry Global Education & Learning Series – 1000 Java Programs.

advertisement
advertisement
If you wish to look at all Java Programming examples, go to Java Programs.

If you find any mistake above, kindly email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.