I had a program written in c + + to combine two order list into a list, I hope you have a look at what is the problem? Here is to define the merge function:
ListNode * Merge (ListNode * Head1, ListNode * Head2)
{
ListNode * p, * q * pa * w;
Q=Head2;
While (q)
{
P=q;
Q=q - & gt; Next;
Pa=Head1;
While (pa)
{
W=pa;
If (p - & gt; E<=pa - & gt; E)
{
//if (p - & gt; E==w - & gt; E)
//break;
p-> Next=pa;
W - & gt; Next=p;
break;
}
The else
Pa=pa - & gt; Next;
}
}
Return w;
}
If you want to test the following is a complete program:
# include & lt; Iostream>
using namespace std;
//define a linked list node
Typedef struct ListNode
{
Int e;
Struct ListNode * Next;
} ListNode;
//insert a new node to the list (on the list head)
Void CreateList (ListNode * & amp; The head, the int data)
{
//create a new node
ListNode * p=(ListNode *) malloc (sizeof (ListNode));
p-> E=data;
p-> Next=NULL;
If (head==NULL)
{
The head=p;
return;
}
p-> Next=head;
The head=p;
}
Void printList (ListNode * head)
{
ListNode * p=head;
While (p!=NULL)
{
cout<& lt; p-> E<" ";
P=p - & gt; Next;
}
cout<& lt; Endl;
}
ListNode * Merge (ListNode * Head1, ListNode * Head2)
{
ListNode * p, * q * pa * w;
Q=Head2;
While (q)
{
P=q;
Q=q - & gt; Next;
Pa=Head1;
While (pa)
{
W=pa;
If (p - & gt; E<=pa - & gt; E)
{
//if (p - & gt; E==w - & gt; E)
//break;
p-> Next=pa;
W - & gt; Next=p;
break;
}
The else
Pa=pa - & gt; Next;
}
}
Return w;
}
Int main ()
{
ListNode * Head1=NULL;
Head2 ListNode *=NULL;
For (int j=5; J> 2; J -)
CreateList (Head1, j);
For (int I=3; i> 0; I -)
CreateList (Head2, I);
Head1=Merge (Head1, Head2);
PrintList (Head1);
//printList (Head2);
system("PAUSE");
return 0;
}