Posts

Showing posts from October, 2010

Merging of linked lists

Image
In this article ,I will be discussing some problems based on merging of linked lists. Problem statement : We had 2 linked lists - a1-a2-a3.....aN and b1-b2-b3..........bN We want to merge it into a single linked list - a1-b1-a2-b2-a3-b3...........an-bn Solution : We would be using the linked list we had already developed in previous blogs. I am again showing the structure of this list below : There would be a simple function in this list which takes 2 nodes and merges them both into the main linked list.The given function is : The simple strategy here is to iterate through the 2 lists and keep on adding current node of of each list to the original linked list. Below is a test program to test it. One variation of the above problem could be to write a function in LinkedList class which accepts a linked list and merges it with itself. Problem statement : Merging of 2 sorted linked lists. Given 2 sorted single linked lists - first : 1-2-3-4-5-8-100-120 second : 5-7-11-34

Some basic linked list problems

Image
In my earlier  post  , we had seen how to implement a basic linked list in C#. We also saw both stack and queue based additions to the linked list. In this post we are going to look at some of the basic linked list problems which will prepare us for more adventure ahead.We will start with basic node class. We would keep adding functions in the LinkedList class as we program. Right now the list is empty with only 2 nodes- head and current which both point to null.Below I am giving the code for simple insertion and printing of nodes,once they are added. Below I am giving code for some functions which can be added to this class. These are : Node Find Find a node in the list.It returns the node when it is found.If the corresponding node is not found null is returned. The next problem is to remove a node from the list. Here we would create a small helper function which would return a previous node. The given functions are below : The next problem is Reversing a list . The next