Join Ex-Google, Ex-Facebook staff software engineer TechLead with daily interview programming problems.
Here's how it works:
|This can be done recursively and iteratively. See if you can get both solutions.|
This problem can be solved recursively or iteratively. We traverse the two linked lists in parallel, advancing both pointers simultaneously. If the first list's value is smaller we advance that one, otherwise we advance the second list. If either list is shorter, then we take values from the longer list.
The time complexity is linear O(n) since both lists are traversed just once. The space complexity of the recursive algorithm is linear O(n), since it builds up a recursive stack that may be as deep as the length of both lists. The space complexity of the iterative solution is constant O(1), since only a few variables are used.
Daily, consistent practice is the best way to get good at solving interview programming questions. In this field, it is a lifelong skill whether you are a beginner or senior software engineer.
There's no catch! Just sign up to start receiving free interview coding problems.
Have a great day!
"TechLead" is Patrick Shyu - ex-Google/ex-Facebook Tech Lead, multi-millionaire app entrepreneur, digital nomad, Silicon Valley native, and senior software engineer. He's held roles in full-stack web development and mobile engineering. He has conducted over 100 interviews at Google, and has worked in the tech industry for over a decade from startups to Fortune 500 companies.