Insert Greatest Common Divisors in Linked List | 2 Approaches | Leetcode 2807 | codestorywithMIK
Whatsapp Community Link : https://www.whatsapp.com/channel/0029...
This is the 28th Video of our Playlist "Array 1D/2D : Popular Interview Problems" by codestorywithMIK
In this video we will try to solve a simple LinkedList Problem : Insert Greatest Common Divisors in Linked List | 2 Ways | Iterative | Recursion | Leetcode 2807 | codestorywithMIK
I will explain the intuition so easily that you will never forget and start seeing this as cakewalk EASYYY.
We will do live coding after explanation and see if we are able to pass all the test cases.
Also, please note that my Github solution link below contains both C++ as well as JAVA code.
Problem Name : Insert Greatest Common Divisors in Linked List | 2 Ways | Iterative | Recursion | Leetcode 2807 | codestorywithMIK
Company Tags : will update soon
My solutions on Github(C++ & JAVA) - https://github.com/MAZHARMIK/Intervie...
Leetcode Link : https://leetcode.com/problems/insert-...
My DP Concepts Playlist : • Roadmap for DP | How to Start DP ? | ...
My Graph Concepts Playlist : • Graph Concepts & Qns - 1 : Graph will...
My Recursion Concepts Playlist : • Introduction | Recursion Concepts And...
My GitHub Repo for interview preparation : https://github.com/MAZHARMIK/Intervie...
Instagram : / codestorywithmik
Facebook : / 100090524295846
Twitter : / cswithmik
Subscribe to my channel : / @codestorywithmik
╔═╦╗╔╦╗╔═╦═╦╦╦╦╗╔═╗
║╚╣║║║╚╣╚╣╔╣╔╣║╚╣═╣
╠╗║╚╝║║╠╗║╚╣║║║║║═╣
╚═╩══╩═╩═╩═╩╝╚╩═╩═╝
Summary :
Approach-1 (Iterative)
Time Complexity (T.C): O(n)
Space Complexity (S.C): O(1)
In this iterative approach, we traverse the linked list using two pointers, currNode and nextNode. For each pair of consecutive nodes, we calculate the GCD of their values and insert a new node with this GCD value between them. The algorithm continues this process until the end of the list is reached. This approach operates in O(n) time, where n is the number of nodes in the list, and uses constant space O(1) since no extra memory is needed aside from the new nodes being inserted.
Approach-2 (Recursive)
Time Complexity (T.C): O(n)
Space Complexity (S.C): O(n) (due to recursive call stack)
In this recursive approach, we traverse the linked list recursively. The function processes the list from the end towards the beginning. For each pair of consecutive nodes, we calculate the GCD of their values, create a new node with this GCD value, and insert it between the current node and the next node. The recursion ensures that nodes are processed in reverse order. This approach also operates in O(n) time, but the space complexity is O(n) due to the system stack space required for recursive function calls.
Comparison:
Iterative Approach: Efficient in terms of space as it uses O(1) extra space, making it ideal for scenarios with memory constraints. However, the logic may be a bit harder to follow compared to recursion.
Recursive Approach: More elegant and easier to understand, but it incurs additional space overhead due to the recursive call stack, making it less optimal for very deep recursion.
✨ Timelines✨
00:00 - Introduction
#coding #helpajobseeker #easyrecipes #leetcode #leetcodequestionandanswers #leetcodesolution #leetcodedailychallenge #leetcodequestions #leetcodechallenge #hindi #india #coding #helpajobseeker #easyrecipes #leetcode #leetcodequestionandanswers #leetcodesolution #leetcodedailychallenge#leetcodequestions #leetcodechallenge #hindi #india #hindiexplanation #hindiexplained #easyexplaination #interview#interviewtips #interviewpreparation #interview_ds_algo #hinglish #github #design #data #google #video #instagram #facebook #leetcode #computerscience #leetcodesolutions #leetcodequestionandanswers #code #learning #dsalgo #dsa #newyear2024