Merge two already sorted linked list This is code to merge two sorted linked lists. l1 and l2 are sorted. While calling the merge function via l3, I am passing l3 as an object by reference in that function. The code is running fine, but can someone suggest how I can code better, with regards to dealing with class objects and passing them in a function?

#include&lt;iostream&gt;
using namespace std;

class linklist{
    private:
        struct node{
            int data;
            node* link;
        }*start;

    public:
        linklist(){
            start=NULL;
        }
        void append(int);
        void display();
        void merge(linklist, linklist,linklist &amp;);
        ~linklist()
        {
            node *q;
            while(start!=NULL)
            {
                q=start-&gt;link;
                delete start;
                start=q;
            }
        }
};
void linklist::append(int num){

    node *temp, *r;
    temp=start;
    if(start==NULL)
    {temp=new node;
    temp-&gt;data=num;
    temp-&gt;link=NULL;
    start=temp;
    }
    else{

        while(temp-&gt;link!=NULL)
        {
            temp=temp-&gt;link;
        }
        r= new node;
        r-&gt;data=num;
        r-&gt;link=NULL;
        temp-&gt;link=r;
    }
}
void linklist::display() {
    node *temp;
    temp=start;
    cout&lt;&lt;"Linked List is"&lt;&lt;endl;
    while(temp!=NULL)
    {
        cout&lt;&lt;temp-&gt;data&lt;&lt;"  ";
        temp=temp-&gt;link;
    }
    cout&lt;&lt;endl;
}

void linklist::merge(linklist l1, linklist l2,linklist&amp; l3) // Here is my doubt
{
    node *p, *q;
    p=l1.start;
    q=l2.start;

    int dat;
    while(p!=NULL &amp;&amp; q!=NULL)
    {
        if(p-&gt;data &gt; q-&gt;data)
        {
            dat=q-&gt;data;
            l3.append(dat);
            q=q-&gt;link;
        }

        else{
            dat=p-&gt;data;
            l3.append(dat);
            p=p-&gt;link;
        }
    }
    if(p==NULL)
    {
        while(q!=NULL)
        {
            dat=q-&gt;data;
            l3.append(dat);
            q=q-&gt;link;
        }
    }
    else{
            while(p!=NULL)
        {
            dat=p-&gt;data;
            l3.append(dat);
            p=p-&gt;link;
        }
    }
    cout&lt;&lt;"lists merged"&lt;&lt;endl;

}

int main()
{
    linklist l1,l2,l3;

    l1.append(2);
    l1.append(5);
    l1.append(23);
    l1.append(34);
    l1.append(45);

    l2.append(6);
    l2.append(9);
    l2.append(35);
    l2.append(98);

    l3.merge(l1, l2,l3);
    l3.display();

    return 0;
}
Here are some things that may help you improve your code.

Don't abuse using namespace std

Putting using namespace std at the top of every program is a bad habit that you'd do well to avoid.  

Use nullptr rather than NULL

Modern C++ uses nullptr rather than NULL.  See this answer for why and how it's useful.  

Use more whitespace

Lines like this one:

cout&lt;&lt;"Linked List is"&lt;&lt;endl;


become much easier to read and understand with a little more whitespace:

cout &lt;&lt; "Linked List is" &lt;&lt; endl;


Use consistent formatting

The code seems to have inconsistent indentation and inconsistent placement of brackets {}.  It doesn't matter as much which convention you use as much as it matters that you follow some convention.  Doing so makes your code easier to read, understand and maintain.

Don't use std::endl if '\n' will do

Using std::endl emits a \n and flushes the stream.  Unless you really need the stream flushed, you can improve the performance of the code by simply emitting '\n' instead of using the potentially more computationally costly std::endl.  For instance, the line quoted above could be written like this:

std::cout &lt;&lt; "Linked List is\n";


Prefer modern initializers for constructors

The constructor use the more modern initializer style rather than the old style you're currently using.  Instead of this:

    linklist(){
        start=NULL;
    }


you could use this:

    linklist() : start{nullptr} {}


This uses the more consistent {} form for initializing start (and so requires a C++11 compiler) but this can be done using older C++99 if needed.  There is not a significant difference in this code, but it's a good habit to get into using.

Use const where practical

The display member functions of linklist does not alter the underlying object and therefore should be declared const:

void display() const;


Make your class destructor virtual

If there's any chance that your class will be derived from, the class destructor should be virtual to avoid problems with collections of objects. See this question for more details on why.  If it can't be derived from, enforce that by declaring the class final.  

With that said, as @isanae has correctly pointed out, you shouldn't be left with the impression that those are the only options.  For a plain old concrete class such as this one, you can reasonably just leave it as it is right now.  

Return something useful from functions

The merge function takes two sorted linked lists and returns a new one; or it should.  Right now it's declared like this

void linklist::merge(linklist l1, linklist l2,linklist&amp; l3)


but it would make much more sense like this:

linklist linklist::merge(linklist l1, linklist l2)


Better still would be for the first list to be implicit so that one could write this:

l3 = l1.merge(l2);


To do that, I'd write the function like this:

linklist linklist::merge(const linklist &amp;l2) const {
    linklist l3;
    node *nodes[2]{start, l2.start};

    while (nodes[0] &amp;&amp; nodes[1]) {
        size_t index = nodes[0]-&gt;data &lt; nodes[1]-&gt;data ? 0 : 1;
        l3.append(nodes[index]-&gt;data);
        nodes[index] = nodes[index]-&gt;link;
    }
    l3 += nodes[0];   // one of the lists is empty at this point
    l3 += nodes[1];
    return l3;
}


Note that this assumes the existence of an operator+= which might be implemented like this:

linklist &amp;operator +=(const node *p) {
    while (p) {
        append(p-&gt;data);
        p = p-&gt;link;
    }
    return *this;
}


Use operator&lt;&lt; instead of display

It would be convenient to be able to redirect the output to something other than std::cout, so the usual idiom is to use something like this:

friend std::ostream&amp; operator&lt;&lt;(std::ostream&amp; out, const linklist &amp;list) {
    for (auto temp=list.start; temp; temp=temp-&gt;link) {
        out &lt;&lt; temp-&gt;data &lt;&lt; "  ";
    }
    return out &lt;&lt; '\n';
}


Now we can use it within main like this:

std::cout &lt;&lt; "finished merging\n" &lt;&lt; l1 &lt;&lt; l2 &lt;&lt; l3;


Consider using a template

As the code is currently written, it can only store a single int as data, but with only a very minor change in the code, it could become a template.  The first few lines could look like this:

template &lt;class T&gt;
class linklist{
private:
    struct node{
        T data;
        node* link;


Thereafter, anywhere you specifically refer to an int for the data, replace it with the templated type T and you now have a generic container.  For your int version, you can write this:

linklist&lt;int&gt; l1;


Consider providing a std::initializer_list constructor

linklist(std::initializer_list&lt;T&gt; list) : start{nullptr} {
    for (auto item : list) {
        append(item);
    }
}


Implementing all of the suggestions above give a main like this:

int main()
{
    linklist&lt;int&gt; l1{2, 5, 23, 34, 45};
    linklist&lt;int&gt; l2{6, 9, 35, 98};

    std::cout &lt;&lt; "Before merging\n" &lt;&lt; l1 &lt;&lt; l2;
    auto l3 = l1.merge(l2);
    std::cout &lt;&lt; "After merging\n" &lt;&lt; l1 &lt;&lt; l2 &lt;&lt; l3;
}


Don't destroy passed objects

Right now the code destroys the passed objects in creating the new list.  That's not a good design, especially because the lists are not even passed by reference, so it would be very surprising to someone looking at the code. That can be fixed with the following suggestion.

Create a copy constructor if you need one

The compiler will create a default copy constructor which does a shallow copy, but this won't work for data structures, like yours, which use pointers.  Instead, if you really want to create a new linklist from an existing one, you'll need to create your own copy constructor:

linklist(const linklist &amp;other) : start{nullptr} {
    for (auto temp = other.start; temp; temp = temp-&gt;link) {
        append(temp-&gt;data);
    }
}


Consider using smart pointers

Using something like a std::unique_ptr would free you (pun intended) from having to manage the mechanics of new and delete explicitly.

Omit return 0

When a C++ program reaches the end of main the compiler will automatically generate code to return 0, so there is no need to put return 0; explicitly at the end of main.