class Solution
{
public:
ListNode *reverseList(ListNode *head){
ListNode *p1=head;
if (p1==nullptr)
{
return nullptr;
}
ListNode *p=new ListNode(-1);
//临时指针模拟头结点
p->next=head;
head=p;
ListNode *current=head->next;
ListNode *pre=nullptr;
ListNode *nextnode;
while (current!=nullptr)
{
nextnode=current->next;
current->next=pre;
pre=current;
current=nextnode;
}
head->next=pre;
return head->next;
}
};
无头结点的反转
最新推荐文章于 2024-11-12 12:29:51 发布