Day 9: I4G 10 Days of Code Challenge.

Photo by Quino Al on Unsplash

Day 9: I4G 10 Days of Code Challenge.

LeetCode Problem 23

This was surprisingly a bit easier than the "hard" rating given to it on LeetCode.

The Problem:

Day 9prob.png

The difference between a linked list and a normal array is quite intuitive. Whereas an array is just a set of values usually separated by commas, a linked list consists of values that are linked together by their nodes.

An array might look like: [3, 4, 4, 6, 7, 7] while a linked list would look like: [3-> 4-> 4-> 6-> 7-> 7]. The problem is to write a program that merges all the linked lists into one. The lists are already sorted, and the merged result is expected to be sorted too.

THOUGHT PROCESS

My first idea was to merge the first 2 lists, then, merge the result with another list and that result with another. I optimised this after some thinking and reading to a solution that merged all the lists in pairs and then merged the results in pairs until a single list is left.

Thus, I divided my program into 2 parts; one part was to handle the iterative function to merge list pairs and the other was to handle the merging function. Before all this, I handled some edge cases (empty lists) by returning a null list.

The Solution:

Day 9soln.png

Ciao!