#include <iostream>
using namespace std;
struct listNode
{
int value;
listNode* next;
};
listNode *reverse(listNode* head)//链表翻转
{
if(head==NULL)//特殊输入处理 要时刻记得处理异常输入
return NULL;
listNode * p1=head;
listNode *p2=head->next;
while(p2!=NULL)
{
listNode *p3=p2->next;//先保存p2的下一个节点
p2->next=p1;//翻转
p1=p2;//往后迭代
p2=p3;
}
head->next=NULL;
return p1;
}
listNode* addNodeToTail(listNode* head,int n ) //在尾部插入元素
{
listNode * newNode=new listNode;
newNode->next=NULL;
newNode->value=n;
if(head==NULL)
{
head=newNode;
return head;
}
listNode *head1=head;
while(head->next!=NULL)
{
head=head->next;
}
head->next=newNode;
链表遍历打印 翻转及 插入元素
最新推荐文章于 2025-03-26 10:13:46 发布