struct ListNode* reverseList(struct ListNode* head) {
struct ListNode*t,*p=NULL,*next=NULL;
t=head;
while(t!=NULL)
{
next=t->next;//首先保存下一结点,防止断连
if(p==NULL)//若当前为第一结点
{
p=t;//p指向当前结点
p->next=NULL;//将最开始进行封装,防止无限指向
t=next;//t指向下一节点
continue;
}
t->next=p;//当前结点指向上一结点
p=t;//p原本为上一结点,现右移
t=next;//t指向下一节点
}
return p;//返回反转后的链表
}
c语言 反向链表
最新推荐文章于 2024-09-26 09:55:13 发布