初学数据结构
ListNode* reverseList(ListNode* head) {
ListNode*cur=head;
ListNode*pre=nullptr;
ListNode*nxt=head->next;
while(cur!=nullptr)
{
cur->next=pre;
pre=cur;
cur=nxt;
nxt=nxt->next;
}
return pre;
}
这是我写的,结果执行出错了
ListNode* reverseList(ListNode* head) {
ListNode*pre=nullptr,*cur=head;
while(cur!=nullptr)
{
ListNode*nxt=cur->next;
cur->next=pre;
pre=cur;
cur=nxt;
}
return pre;
}
这是正确的。不明白nxt的定义为什么一定要放到循环里面,总感觉我那样写和这种正确写法没有区别,实在是看不出来为什么,跪求大佬指教!!!