Today I solved LeetCode 21: Merge Two Sorted Lists, a classic linked-list problem that is great for strengthening pointer manipulation and understanding how linked lists work.
The goal is simple: given two sorted linked lists, merge them into a single sorted linked list.
For example:
Input:
List 1: [1,2,4]
List 2: [1,3,4]
Output:
[1,1,2,3,4,4]
I solved this using a dummy node and a current pointer. The dummy node makes the implementation cleaner because it provides a starting point without needing special handling for the first element.
At every step, I compare the values of the two current nodes. The smaller value is attached to the merged list, and its pointer moves forward. Once one list reaches the end, the remaining portion of the other list is directly connected.
This approach is efficient because each node is visited only once.
⏱️ Time Complexity: O(n + m)
💾 Space Complexity: O(1) extra space
The submission passed all 208 test cases with a runtime of 0 ms, making it a great practice problem for mastering linked lists and pointer-based algorithms.
Every problem solved is another step toward becoming a better problem solver and developer. 💻🔥
#LeetCode #JavaScript #DSA #DataStructures #Algorithms #LinkedList #Coding #Programming #ProblemSolving #100DaysOfCode #SoftwareDevelopment #FrontendDeveloper #WebDevelopment #CodeNewbie #DeveloperJourney