Introduction
In this article, I am going to explain how to merge 2 sorted linked lists. We are given 2 sorted linked lists, and we had to merge them into one such that the final list is also a sorted one.
How to merge LinkedList?
However, in C#, we already have a LinkedList class under System.Collections.Generic namespace, it is always good to build it from the ground up since it helps in understanding fundamentals. Now a singly linked list is a collection of nodes where one node points to another and the last one points to null. Though there is no concept of pointers in C#, internal addresses are managed through pointers only.
So let's define a simple Node class.
public class Node
{
public object data;
public Node next;
public Node(object data)
{
this.data = data;
}
}
The Node class is a very simple class that contains some data and has an element Node that points to the next node.
A linked list will be simply a collection of these nodes.
Let's define it also.
public class LinkedList
{
Node head;
Node current;
public Node Head //Expose a public property to get to head of the list
{
get { return head; }
}
public void Add( Node n)
{
if (head == null)
{
head = n; // point head to first added node
current = head; // set current to head
}
else
{
current.next = n; //Set current next to newly added node.
current = current.next; //Set new current to current next.
}
}
}
This linked list class has 2 nodes- head and current. Node head points to the beginning of a list, and current denotes the current node in the list chain. Also, we had a public method, Add(), which adds a node to the end of the linked list.
Now coming to the problem. We had 2 sorted linked lists, and we had to merge them into one, which again should be sorted.
e.g. say first list is 1-3-5-6-8-10 and other is 5-7-8-9-15-20 then final list should be 1-3-5-5-6-7-8-9-10-15-20
There can be many ways to do this, but here we will be focussing on merging only. The way we would be progressing is to write a function in the LinkedList class, which takes 2 nodes and returns the final merged list. A small pseudo algorithm for the above solution would be:
Find out which list's head is less. Assign that list as the first and another one as the second
e.g. For the above cases of list first list would be 1-3-5..... and the second one would be 5-7-8.. if the lists have been 3-5-6-8-10 and 2-5-7-8-9..... then we would have assigned 2-5-7...as first and another one as the second.

Abhishek KumarPosted Feb 7, 2023, 10:11 AM
Line 5 if(Convert.ToInt32(first.next.data.ToString()) .. i t should be first.data not next data...
Gaurav RawatPosted Apr 27, 2012, 1:07 AM
Karthik :Have you put the function MergeSortedList inside the class LinkedList?
karthik parchaPosted Apr 26, 2012, 7:03 AM
Mergesortedlist fucntions is not showing in intellisence of main() showing error like 'linked_list.Program.LinkedList' does not contain a definition for 'MergeSortedList' and no extension method 'MergeSortedList' accepting a first argument of type 'linked_list.Program.LinkedList' could be found (are you missing a using directive or an assembly reference?)
Sam HobbsPosted Oct 27, 2010, 4:58 AM
This reminds me of the old times I programmed using tape drives. We often wrote applications that needed to match or merge data from two or more tapes. The capacity of disk drives were too limited for things such as credit card master files or aircraft parts lists. Often transactions would be matched to the master file for updates. For me, I am happy to use general-purpose software developed by others to do things such as this, but if someone needs to do something custom that existing software cannot do then this is a good start.