IP #8 - The List Goes on! Merging two sorted lists.

Merging two sorted lists

by Ashur Baroutta


    This week we explore merging two sorted linked lists. My first try at this problem looks wildly different than the final submission. At first I tried creating a new list first, then I setup a loop to iterate and check the nodes within list 1 and list 2 and kept adding them to the newly created list one by one. To be fair, that approach worked fine, nothing wrong with it and my submission was accepted. Though its speed was low and the memory usage was even worse than my final submission. 

    So I got annoyed and started looking more into how to optimize or change up how im solving the problem. I saw recursion as a better way to go about getting better performance. Recursion is when a method calls itself within its execution. So with recursively iteratingthrough each node, I was able to get a huge leg up on my first submission in terms of performance. Satisfied with the performance in terms of speed I'm still lacking an edge as far as memory efficiency goes. 


    
    The simplest solution I could think of to get better memory usage was to create reference variables to the lists being passed in, instead of acting directly on the passed lists before referencing them. It worked!


    Huge gap between the two in terms of memory usage and all I had to do was add two lines of code. The reason why it helps to use the reference variables is that its referencing the data being passed in, not acting on the entire dataset every time the list is called. Though my understanding might not be 100% accurate, I think that is the case. 

Comments

Popular posts from this blog

IP #6 : Reversing an Integer

IP#5 : LinkedList Removing Duplicates II