user-0 提问: 有序链表的合并出错了，请各位大神提供指教！

回复:
user: user-0
content: 引用 1 楼 自信男孩的回复:#include<stdio.h> #include<stdlib.h> struct node {     int data;     struct node *next; };  typedef struct node NODE;  NODE* creat(); void print(NODE *head); NODE* sort(NODE *heada,NODE *headb);  int main() {     NODE *heada,*headb,*headc;     heada = creat();     print(heada);     headb = creat();     print(headb);     headc = sort(heada, headb);     print(headc);      return 0; }  NODE *creat() {     NODE *head,*p,*q;     int n, i;     printf("\ninput n:");     scanf("%d", &n);     q = head = (NODE *)malloc(sizeof(NODE));     q->next = NULL;     printf("\ninput data:");     for(i = 0; i < n; i++)     {         p = (NODE*)malloc(sizeof(NODE));         scanf("%d",&p->data);         p->next = NULL;         q->next = p;         q = p;     }     return(head); } void print(NODE *head) {     NODE *a;     a = head->next;    /* error appear here*/      while (a) {         printf("%d ",a->data);         a = a->next;     }     printf("\n"); } NODE *sort(NODE *heada, NODE *headb) {     NODE *p, *q, *s, *headc, *tmp = NULL;      headc = s = heada;      /* must value p and q */     p = heada->next;     q = headb->next;     while (p && q) {         if(p->data < q->data) {             s->next = p;             s = p;             p = p->next;         } else if (p->data > q->data){             s->next = q;             s = q;             q = q->next;         } else {    /*数据相等，链入一个，释放另一个*/             s->next = p;             s = p;             p = p->next;             tmp = q;             q = q->next;             free(tmp);         }     }      while (q) {    /*剩余的B链表*/         s->next = q;         s = q;         q = q->next;     }     while (p) {    /*/*剩余的A链表*/*/         s->next = p;         s = p;         p = p->next;     }     free(headb);     return(headc); } 根据函数名和功能相匹配，所以，不建议在sort函数里去创建链表，不然函数比较不容易理解； 以上是修改后的代码，部分代码已有注释，对比你的看一下吧； 谢谢提醒！

user: user-1
content: #include<stdio.h> #include<stdlib.h> struct node {     int data;     struct node *next; };  typedef struct node NODE;  NODE* creat(); void print(NODE *head); NODE* sort(NODE *heada,NODE *headb);  int main() {     NODE *heada,*headb,*headc;     heada = creat();     print(heada);     headb = creat();     print(headb);     headc = sort(heada, headb);     print(headc);      return 0; }  NODE *creat() {     NODE *head,*p,*q;     int n, i;     printf("\ninput n:");     scanf("%d", &n);     q = head = (NODE *)malloc(sizeof(NODE));     q->next = NULL;     printf("\ninput data:");     for(i = 0; i < n; i++)     {         p = (NODE*)malloc(sizeof(NODE));         scanf("%d",&p->data);         p->next = NULL;         q->next = p;         q = p;     }     return(head); } void print(NODE *head) {     NODE *a;     a = head->next;    /* error appear here*/      while (a) {         printf("%d ",a->data);         a = a->next;     }     printf("\n"); } NODE *sort(NODE *heada, NODE *headb) {     NODE *p, *q, *s, *headc, *tmp = NULL;      headc = s = heada;      /* must value p and q */     p = heada->next;     q = headb->next;     while (p && q) {         if(p->data < q->data) {             s->next = p;             s = p;             p = p->next;         } else if (p->data > q->data){             s->next = q;             s = q;             q = q->next;         } else {    /*数据相等，链入一个，释放另一个*/             s->next = p;             s = p;             p = p->next;             tmp = q;             q = q->next;             free(tmp);         }     }      while (q) {    /*剩余的B链表*/         s->next = q;         s = q;         q = q->next;     }     while (p) {    /*/*剩余的A链表*/*/         s->next = p;         s = p;         p = p->next;     }     free(headb);     return(headc); } 根据函数名和功能相匹配，所以，不建议在sort函数里去创建链表，不然函数比较不容易理解； 以上是修改后的代码，部分代码已有注释，对比你的看一下吧；