Add Numbers Represented by Linked Lists in Java26 Sept 2024 | 8 min read It is a very interesting problem frequently asked in interviews of top IT companies like Google, Amazon, TCS, Accenture, etc. By solving the problem, one wants to check the logical ability, critical thinking, and problem-solving skill of the interviewee. So, in this section, we are going to solve how to add numbers represented by linked list in Java with different approaches and logic. Also, we will create Java programs for the same. Problem StatementTwo linked lists are given. Each linked list represents a number. The task is to find the sum of the numbers represented by each linked list. The answer has to be in the form of a linked list. Observe the following examples. Example 1: Input: L1 = 7 -> 9 -> 0 -> 5 -> 9 -> 1 -> 2 -> NULL L2 = 5 -> 3 -> 8 -> 4 -> 3 -> 6 -> 4 -> NULL Output: 1 -> 3 -> 2 -> 9 -> 0 -> 2 -> 7 -> 6 -> NULL Explanation: The number represented by the list L1 is 7905912 and the number represented by the list L2 is 5384364, and their sum is 7905912 + 5384364 = 13290276. Example 2: Input: L1 = 9 -> 8 -> 0 -> 4 -> 3 -> 6 -> 2 -> NULL L2 = 8 -> 5 -> 7 -> NULL Output: 9 -> 8 -> 0 -> 5 -> 2 -> 1 -> 9 -> NULL Explanation: The number represented by the list L1 is 9804362 and the number represented by the list L2 is 857, and their sum is 9804362 + 857 = 9805219. Solution to the ProblemApproach: Using RecursionWe know that the sum of two numbers can be found by adding digits from right to left. Therefore, it is required to reverse the given linked lists. Though, we will not be literally reversing the linked list. We will imitate the same using recursion. We will do the basic sum that we have learned in our primary classes. After finding the sum, construct a new linked list that shows the sum. Illustration of the same is mentioned below. FileName: NumberSumLinkedList.java Output: First List is 7 9 0 5 9 1 2 Second List is 5 3 8 4 3 6 4 Resultant List is 1 3 2 9 0 2 7 6 First List is 9 8 0 4 3 6 2 Second List is 8 5 7 Resultant List is 9 8 0 5 2 1 9 Complexity Analysis: Since for the addition, we have to traverse both lists. Therefore, the time complexity of the program is O(a + b), where a is the total number of elements present in the first linked list, and b is the total number of elements present in the second linked list. The space complexity of the program is also O(a + b), as we also need to store the resultant linked list. Approach: Using IterationIn this approach, we will be using a stack to store the elements of both linked lists. We know that a stack works in a Last In First Out (LIFO) manner. Therefore, the last node will be popped out first. On the popped elements from both the linked list, we do the add operation. FileName: MajorityEle1.java Output: First List is 7 9 0 5 9 1 2 Second List is 5 3 8 4 3 6 4 Resultant List is 1 3 2 9 0 2 7 6 First List is 9 8 0 4 3 6 2 Second List is 8 5 7 Resultant List is 9 8 0 5 2 1 9 Complexity Analysis: The time, as well as the space complexity of the program is the same as the above. Next TopicMajority Element In an Array in Java |
Java program to print the following pattern on the console To accomplish this task, we need to create two loops and the 2nd loop is to be executed according to the first loop. The first loop is responsible for printing the line breaks whereas the second...
1 min read
Java Convert Octal to Decimal We can convert octal to decimal in java using Integer.parseInt() method or custom logic. Java Octal to Decimal conversion: Integer.parseInt() The Integer.parseInt() method converts a string to an int with the given radix. If you pass 8 as a radix, it converts an octal...
2 min read
The natural numbers are the numbers that include all the positive integers from 1 to infinity. For example, 1, 2, 3, 4, 5, ......, n. When we add these numbers together, we get the sum of natural numbers. In this section, we will create the following programs: Java...
3 min read
In this program, we need to swap two strings without using a third variable. Str1: Good Str2: morning Swapping two strings usually take a temporary third variable. One of the approach to accomplish this is to concatenate given two strings into first string. Str1Str1 = Str1 + Str2=...
2 min read
Java Convert int to char We can convert int to char in java using typecasting. To convert higher data type into lower, we need to perform typecasting. Here, the ASCII character of integer value will be stored in the char variable. To get the actual value in char...
2 min read
Linked List can be defined as a collection of objects called nodes that are randomly stored in the memory. A node contains two fields, i.e. data stored at that particular address and the pointer which contains the address of the node in the memory. The last...
2 min read
In this program, we will create a circular linked list and insert every new node at the end of the list. If the list is empty, then head and tail will point to the newly added node. If the list is not empty, the newly...
6 min read
? It is a very interesting problem frequently asked in interviews of top IT companies like Google, Amazon, TCS, Accenture, IBM etc. By solving the problem, one wants to check the logical ability, critical thinking, and problem-solving skill of the interviewee. So, in this section, we are...
2 min read
We can capitalize each word of a string by the help of split() and substring() methods. By the help of split("\\s") method, we can get all words in an array. To get the first character, we can use substring() or charAt() method. Let's see the example...
1 min read
Java Convert Decimal to Octal We can convert decimal to octal in java using Integer.toOctalString() method or custom logic. Java Decimal to Octal conversion: Integer.toOctalString() The Integer.toOctalString() method converts decimal to octal string. The signature of toOctalString() method is given below: public static String toOctalString(int decimal) Let's see the simple example...
2 min read
We request you to subscribe our newsletter for upcoming updates.
We provides tutorials and interview questions of all technology like java tutorial, android, java frameworks
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India