Update appNew update is available. Click here to update.
sidenav-btnClose
Topic list
Add Two Numbers As Linked Lists ll
MEDIUM
10 mins
188 upvotes
Linked List
Topics (Covered in this problem)
Problem solved
Badge
Skill meter
Linked List
-
-
Other topics
Problem solved
Badge
Skill meter
Strings
-
-
Matrices (2D Arrays)
-
-
Sorting
-
-
Binary Search
-
-
Stacks & Queues
-
-
Trees
-
-
Graph
-
-
Dynamic Programming
-
-
Greedy
-
-
Tries
-
-
Arrays
-
-
SQL
-
-
Binary Search Trees
-
-
Heap
-
-
Bit Manipulation
-
-
Solve problems & track your progress
Checkout your overall progress in every topic here
Become
userLevel
Sensei
in DSA topics
Open the topic and solve more problems associated with it to improve your skills
Check out the skill meter for every topic
See how many problems you are left with to solve for cracking any stage. Score more than zero to get your progress counted.

Add Two Numbers As Linked Lists ll

Contributed by
Deep Mavani
Medium
yellow-spark
0/80
Avg time to solve 10 mins
Success Rate 80 %
Share
188 upvotes

Problem Statement

You have been given two singly Linked Lists, where each of them represents a positive number without any leading zeros.

Your task is to add these two numbers and print the summation in the form of a linked list.

Example:
If the first linked list is 1 -> 2 -> 3 -> 4 -> 5 -> NULL and the second linked list is 4 -> 5 -> NULL.

The two numbers represented by these two lists are 12345 and 45, respectively. So, adding these two numbers gives 12390. 

So, the linked list representation of this number is 1 -> 2 -> 3 -> 9 -> 0 -> NULL.
Detailed explanation ( Input/output format, Notes, Constraints, Images )
Sample Input 1 :
2
1 1 -1
9 9 9 -1
2 4 -1
5 3 -1
Sample Output 1:
1 0 1 0 -1
7 7 -1
Explanation for Sample Output 1:
In test case 1, we are adding 11 and 999 to get 1010.

In test case 2, we are adding 24 and 53 to get 77.
Sample Input 2:
2
3 8 1 2 9 -1
9 8 2 9 -1
1 9 0 -1
8 1 0 -1
Sample Output 2:
4 7 9 5 8 -1
1 0 0 0 -1
Explanation for Sample Output 2:
In test case 1, we are adding 38129 and 9829 to get 47958.

In test case 2, we are adding 190 and 810 to get 1000.
Reset Code
Full screen
Auto
copy-code
Console