LeetCode(Problem Of Day)
Avnish Kumar ???? ???? ????
Engineering @ Zepto | Ex- Acko | Exploring world of Generative AI & Web3 | Competitive Programming (DSA ) | Experience in Developing ( Frontend & Backend ) | Automation ( Mobile & Web & API )
Initials thinking for the problem :-
1.Just noobs thinking(common person) by seeing the statement , reverse the list L1 and L2 , then calculate the sum , then reverse that calculated sum which finally give our answer , as common addition of two number.
2. As we see if we follow the step -1 approach we required to reverse the first two given Linked list L1 and L2 which going to take O(L1)+O(L2) + O( output result length) and much more.
3. As a programmer we drop this initial though on looking to Time Optimization , then we see the i/o we get idea we iterate the list L1 and L2 , and adds up , if carry is generated then , we move the carry in creation of next node and adds the value up their.
Now the Algorithm we Apply for this:-
Time and Space Complexity:-
领英推荐
Time:- O(max(L1,L2)):-> Time is the max length of both of L1 and L2 , as we have to iterate over the both list fully.
Space :- O(N) :->To creating the new Linked List contains the summation of both L1 and L2.
Problem Link:- https://leetcode.com/problems/add-two-numbers/
Code:-