Addition of two linked lists Follow up (Data Structures and Algorithms #35)(Linked List #9)

Опубликовано: 11 Июль 2026
на канале: Code Campaign
885
25

This video explains how to add two linked lists representing numbers. Here we will discuss the most efficient solution which take constant space. To understand this solution completely please look into video:    • Addition of two linked lists. (Data Struct...  .

Constraints:
1.) Linkedlist is immutable. (Can not do any modification in linked list)
2.) Linkedlist is really huge (billions of nodes), can not be completely loaded into main memory.

This constraint takes this problem to another new level. Mostly interviewer use this question as follow up question to understand candidate's understanding of the domain. Also, this question gives great insight into the candidate's ability to best use the information given to him/her in the problem statement and use this information to come up with an really optimized solution.

To reiterate, there are two key rules of this algorithm:
1.) Move carry pointer to new node if new node value is less than 9.
2.) If sum of lists nodes is greater than 9 then increment value of the node by 1 which is being pointed by carry pointer. Keep on moving forward until find the node which has the value less than 9. Any node which have the value equal to 9, change the value of that to 0.

Analysis of Algorithm:
Time Complexity: O(n)
Space Complexity: O(1)

Code Repository Link: https://github.com/HimanshuVerma18111...