Javatpoint Logo
Javatpoint Logo

Add two numbers represented by linked lists

Linked list

In Computer Science, linked list is a data structure where data is stored in a linear fashion but not stored in the contiguous memory location. There is a series of nodes connected where each node contains the data value and address of the next value.

Problem statement:

Here, we are given two linked lists, and we have to add these two linked lists considering them as two numbers, and return the sum in the form of linked lists.

For example:

We have two linked lists like:

Add two numbers represented by linked lists

In the above example, list1 represents the number 2342, and list2 represents the number 95607. If we sum up these two numbers, we will get the result as 97949, and we will return this number as a linked list.

The most significant digit of any number will be the head of the linked list, and the least significant digit of the number will be the last node of the linked list.

Method 1

The most straightforward approach to solve the problem is that we will convert both lists into equivalent integer values, and we will store the sum of these two numbers into an integer value. Now we will convert this integer into a linked list.

Java Code:


Add two numbers represented by linked lists


  • In the above code, we have implemented a custom class for the node and list. With the help of a custom class, we created two linked lists.
  • Now, we use two functions, one function is to get the integer equivalent of a linked list, and another function is to create the linked list from the given integer.
  • To get the integer equivalent of a linked list, we first find out the size of the linked list by traversing it. After fitting the number of nodes which will also be the number of digits in the number, we traverse again in the list and add the value to the answer with the appropriate power of 10 according to their index.
  • After getting the integer equivalent of both numbers, we get the sum and then convert that sum into the list. The second function is returning the head of the Linked list.
  • In this function, we use the recursive approach to create the list. We created the node for the last digit of the number and called the recursive function for the remaining values. After getting the head node of the remaining values, we added the current node at the end of the list we got from the remaining number.
  • For the base case, if the number remains a single digit, we will create the node and just return that node.
  • The time complexity of the above code: would be O(N), where N is the maximum number of elements in both lists.
  • The space complexity of the above code would be: O(N)

Method 2

We will traverse both lists and add the values during traversal.

In this approach, we will reverse both lists and start adding elements. We will maintain a carry variable which will be propagated in the next iteration again and again. If carry remains 1 in the end, we will create the extra node of that carry. Else, we will not since the resultant linked list will be in the reverse order, so we will reverse it again to get the actual order.

Java code:

Time complexity: O(N)

Space complexity: O(N)

Youtube For Videos Join Our Youtube Channel: Join Now


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Trending Technologies

B.Tech / MCA