GeeksforGeeks App
Open App
Browser
Continue

# Subtract Two Numbers represented as Linked Lists

Given two linked lists that represent two large positive numbers. Subtract the smaller number from the larger one and return the difference as a linked list. Note that the input lists may be in any order, but we always need to subtract smaller ones from larger ones.
Note: It may be assumed that there are no extra leading zeros in input lists.

Examples:

Input: l1 = 1 -> 0 -> 0 -> NULL,  l2 = 1 -> NULL
Output: 0->9->9->NULL
Explanation: Number represented as
lists are 100 and 1, so 100 - 1 is 099

Input: l1 = 7-> 8 -> 6 -> NULL,  l2 = 7 -> 8 -> 9 NULL
Output: 3->NULL
Explanation: Number represented as
lists are 786 and  789, so 789 - 786 is 3,
as the smaller value is subtracted from
the larger one.

Approach: Following are the steps.

1. Calculate sizes of given two linked lists.
2. If sizes are not the same, then append zeros in the smaller linked list.
3. If the size is the same, then follow the below steps:
1. Find the smaller valued linked list.
2. One by one subtract nodes of the smaller-sized linked list from the larger size. Keep track of borrow while subtracting.

Following is the implementation of the above approach.

## Javascript



Output

0 9 9

Complexity Analysis:

• Time complexity: O(n).
As no nested traversal of linked list is needed.
• Auxiliary Space: O(n).
If recursive stack space is taken into consideration O(n) space is needed.

The approach is similar to this
The idea is to use 10s complement arithmetic to perform the subtraction.

Number1 – Number2 = Number1 + (- Number2) = Number1 + (10’s complement of Number2)

Number1 – Number2 = Number1 + (9’s complement of Number2) + 1

The 9’s complement can be easily calculated on the go by subtracting each digit from 9. Now, the problem is converted to addition of two numbers represented as linked list.

Follow the steps below to implement the above idea:

1. Remove all the initial zeroes from both the linked lists.
2. Calculate length of both linked list.
3. Determine which number is greater and store it in list 1 (L1) and smaller one in list 2 (L2)
4. Now perform addition of linked list while converting L2 to 10’s complement
4.1 Initialize carry = 1 because 10’s complement = 9’s complement + 1
4.3 For each node, add the value of L1 and 9’s complement of value of L2 and carry i.e. sum = (L1 value) + (9 – L2 value) + carry
4.4 Calculate carry = (sum / 10) and sum = (sum % 10) and store sum in a new node
5. After getting the resulting list, reverse it and remove initial zeroes
6. If resulting list becomes empty, add a new node with value 0 (zero), otherwise the remaining list is the answer

Below is the implementation of the above approach:

## C++

Output

9 9

Complexity Analysis:

• Time complexity: O(n).
As no nested traversal of linked list is needed.
• Auxiliary Space: O(n).
O(n) space is needed to store the resultant list, but it can be made to O(1) space by storing the result in original linked list.

This approach is contributed by Piyush Garg (infinity4321cg)

This article is contributed by Mu Ven. If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.