C++ 两个链表合并为第三个链表,并按顺序输出(求指点!)

C++ 两个链表合并为第三个链表,并按顺序输出(求大虾指点!)
#include <stdio.h>
#include <stdlib.h>

typedef struct LNode
{
char data;
struct LNode *next;
}Lnode,*LinkList;

void CreateList(LinkList &L,int n) {
L = (LinkList)malloc(sizeof(LNode));
LinkList p;
L->next = NULL;
for(int i = n; i >0; --i) {
p = (LinkList)malloc(sizeof(LNode));
scanf("%d",&p->data);
p->next = L->next; L->next = p;
}
}

void sortlist(LinkList &L1,LinkList &L2,LinkList &L3)
{
LinkList p1,p2,p3;
p1 = L1->next;
p2 = L2->next;
L3 = p3 = L1;
p3->next=NULL;
while(p1 && p2)
{
if(p1->data <= p2->data){
//p3->next = p1;
p3 = p1;
p1 = p1->next;

}
else
{
//p3->next = p2;
p3 = p2;
p2 = p2->next;

}
//p3->next = p1?p1:p2;

p3->next = L1->next;
L1->next=p3;
}

while(p1)
{
p3=p1;
p1=p1->next;
p3->next=L1->next;
L1->next=p3;
}
while(p2)
{
p3=p2;
p2=p2->next;
p3->next=L1->next;
L1->next=p3;
}

//return L3;
//free(L2);
}

int main()
{
LinkList p,q,l1,l2,l3,z;

CreateList(l1,3);
p = l1->next;
while(p)
{
printf("%d ",p->data);
p=p->next;
}
printf("\n");

CreateList(l2,5);
q = l2->next;
while(q)
{
printf("%d ",q->data);
q=q->next;
}
printf("\n");

sortlist(l1,l2,l3);

z = l3->next;
while(z)
{
printf("%d ",z->data);
z=z->next;
}

  return 0;
}

为什么我这个没排序?

------解决方案--------------------
合并两个链表的考点 就在于: 引入一个dummy node,这样保证新链表的链表尾即为 插入点。

更多请参考: http://www.geeksforgeeks.org/archives/3622


Merge two sorted linked lists
May 27, 2010

Write a SortedMerge() function that takes two lists, each of which is sorted in increasing order, and merges the two together into one list which is in increasing order. SortedMerge() should return the new list. The new list should be made by splicing together the nodes of the first two lists.

For example if the first linked list a is 5->10->15 and the other linked list b is 2->3->20, then SortedMerge() should return a pointer to the head node of the merged list 2->3->5->10->15->20.

There are many cases to deal with: either ‘a’ or ‘b’ may be empty, during processing either ‘a’ or ‘b’ may run out first, and finally there's the problem of starting the result list empty, and building it up while going through ‘a’ and ‘b’.

Method 1 (Using Dummy Nodes)
The strategy here uses a temporary dummy node as the start of the result list. The pointer Tail always points to the last node in the result list, so appending new nodes is easy.
The dummy node gives tail something to point to initially when the result list is empty. This dummy node is efficient, since it is only temporary, and it is allocated in the stack. The loop proceeds, removing one node from either ‘a’ or ‘b’, and adding it to tail. When
we are done, the result is in dummy.next.

/*Program to alternatively split a linked list into two halves */
#include<stdio.h>
#include<stdlib.h>
#include<assert.h>

/* Link list node */
struct node
{
int data;
struct node* next;
};
/* 说明: 一般的链表数据结构都是有数据元素的,也有全是指针的,称作链。。。*/
/* pull off the front node of the source and put it in dest */
void MoveNode(struct node** destRef, struct node** sourceRef);