/*
Reverse a linked list and return pointer to the head
The input list will have at least one element
Node is defined as
struct Node
{
int data;
struct Node *next;
}
*/
Node* Reverse(Node *head)
{
Node *p,*pPrev;
if(!head||!head->next) //链表为空或者只有一个元素
return head;
p=head->next;
head->next=NULL; //尾结点
while(p)
{
pPrev=p;
p=pPrev->next;
pPrev->next=head;
head=pPrev;
}
return head;
}
HackerRank Reverse a linked list(逆置链表)
最新推荐文章于 2022-08-26 15:41:30 发布