Now inside your directory, you can create a file called mergedTwoLists.js and. (In your directory, make sure you have run npm init first to create a package.json file)! To use jest in Node.js, all you need to do is create a directory, and in that directory run npm i jest to save it as a dev dependency, npm i - save-dev jest. If you clearly know what the inputs look like and what the return value should be, you’re already done with 30% of the problem in my opinion, as this also gives you a chance to think about edge cases. Building your own tests is not only an invaluable skill for a software engineer, but it also allows you to think more deeply about the code you are going to need to solve the algorithm. I find performing your own tests, to be very worthwhile when it comes to algorithms. With the inputs and outputs in mind, it becomes much easier to build out tests which I’ll do now using jest. Lastly, the size of the returned linked list is the sum of the nodes in each list passed in. Secondly, we are returning a linked list. Firstly, we are receiving two linked lists as arguments and they are sorted. The new list should be made by splicing together the nodes of the first two lists.”įrom the description, I notice a few things in particular. ![]() “Merge two sorted linked lists and return it as a new list. Here is the description Leetcode gives us for this problem along with their example input and output: With all of this in mind, let’s now look at the problem. Linked (no pun intended) below is a really great site that visualizes methods we can perform on a linked node. ![]() In a doubly linked list however, the head node’s previous node pointer, like the tail pointers next node pointer would be pointing to null. The tail is the end of a linked list with a value and a pointer pointing to null. The head node is the start of a linked list, containing a value and a pointer to the next node. In a doubly linked list, each node holds a value and a pointer to a previous node and another node. ![]() In a singly linked list, each node holds a value and a pointer to another node in the chain. As I dig my heels into algorithms and data structures, I felt as though this problem was particularly helpful in elucidating a lot about Linked Lists as a data structure, so I thought it would help to do a walkthrough of the problem along with some added tips for algorithm practice I’ve found super helpful.Ī Linked List is a linear data structure, like an array, a stack, or a queue, meaning that data is accessed sequentially one after the other in an ordered fashion. In the classic Leetcode algorithm titled “21: Merge Two Sorted Lists” we are to take two sorted linked lists and merge them into one sorted linked list.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |