合并两个有序单链表时,输出时一直循环

#include <stdio.h>
#include <stdlib.h>

//声明struct student类型
typedef struct student{
int data;
struct student * next;
}LNode,*LinkList;
LinkList L1,L2,L3;

LinkList create(void){
LinkList head;
LNode *p1,*p2;
int a,n;
head=NULL;
printf("几个:");
scanf("%d",&n);
for(int i=0;i<n;i++){
scanf("%d",&a);

    p1=(LNode *)malloc(sizeof(LNode));  //p1为新建指针
    p1->data=a;  //p1数据域赋值
    if(head==NULL){
        head=p1;
    }else{
        p2->next=p1;  //p2指向链表尾部
    }
    p2=p1;
   
}
p2->next=NULL;  //尾部的指针的next指针置空
return head;

}

LinkList hebin(LinkList L1,LinkList L2){

LNode *prev;
while (L1!=nullptr&&L2!=nullptr) {
    if(L1->data<L2->data){
        prev->next=L1;
        L1=L1->next;
    }
    else{
        prev->next=L2;
        L2=L2->next;
    }
    prev=prev->next;
}
prev->next=L1==nullptr ?L2 :L1;
return L1;

}

int main()
{

L1=create();
L2=create();
hebin(L1,L2);  //连接两个链表
printf("合并后的链表是:\n");
while(L1){
    printf("%d",L1->data);
    L1=L1->next;
    
}
return 0;

}