Cracking the Coding Interview: Merge two sorted lists Python.

This is another leetcode easy level problem and it is fundamental to as well learn how to quickly solve this when asked in an interview setting.

The Question:

Merge two sorted linked lists and return it as a new list. The new list should be made by splicing together the nodes of the first two lists.Example:Input: 1->2->4, 1->3->4Output: 1->1->2->3->4->4

Problem solution approach

We should create a new linked list and initialize a zero pointer that points to the first node. We then iterate using the pointer continuously adding nodes in the linked list while traversing both lists.

Here is the code:

Note: We use while true to iterate indefinitely and then break out of the while loop if a certain condition is met

And that is a simple way of approaching this problem. Next we are going to look at merge k sorted lists.

See you on the next coding interview problem!

Software engineer. I write about my journey, Machine Learning, Web application development and also Python!

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store