这种复制链表的方法是否正确?

void InsertAtTail(struct node** headref,int val) { struct node *current,*newnode; current=*headref; newnode=malloc(sizeof(struct node)); if(current==NULL) { newnode->data=val; newnode->next=NULL; *headref=newnode; current=*headref; } else { while(current->next!=NULL) { current=current->next; } newnode->data=val; newnode->next=NULL; current->next=newnode; } } struct node* CopyList(struct node* headref) { struct node* newlist=NULL; struct node* current; current=headref; if(current==NULL) { newlist=current; } else { while(current!=NULL) { InsertAtTail(&newlist, current->data); current=current->next; } } return (newlist); } 

我正在浏览斯坦福大学的CS101笔记,并找到了制作链表副本的代码。 但它也使用了指向尾节点的指针。 我没有使用它(尾指针)编写了这段代码。 我是链接列表的新手。 请告诉我是否可以这样做。 当我打印原件和副本的地址时,两者也不同。 我在Xcode中使用c。

工作正确,但更短:

 void InsertAtTail(struct node** ref,int val) { while (*ref != NULL) { ref = &(*ref)->next; } struct node *newnode = malloc(sizeof(struct node)); newnode->data=val; newnode->next=NULL; *ref = newnode; } 

并且应该重写列表复制:N²/ 2。