top of page

Program to merge two sorted linked list in C#.

In this article, we will discuss how to merge two sorted linked list.

This is a frequently asked interview question. This can be solved by comparing each node of first list with second one and inserting nodes from second to first where ever there is a need.

public void MergeSortedList(Node first, Node second) {   
    //we would be adding node from second list to first list   
    //If second node data id more than first one then exchange it   
    if( > 0)   
        node t = first;    
        first = second;    
        second = first;   
head = first;//Assign head to first Node  

while (( != null ) && (second != null)) {      
    if (Convert.ToInt32( < Convert.ToInt32(      
    first =;//iterate over the first node      
    Node n =;      
    Node t =;  = second;  = n;      
    first =;      
    second = t;      
if ( = null)     
{ =  second;     

Source: Csharpstar

The Tech Platform



bottom of page