【发布时间】:2022-08-18 15:48:51
【问题描述】:
我正在尝试反转具有以下代码的linked list。
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode() : val(0), next(nullptr) {}
* ListNode(int x) : val(x), next(nullptr) {}
* ListNode(int x, ListNode *next) : val(x), next(next) {}
* };
*/
class Solution {
public:
ListNode* reverseList(ListNode* head) {
ListNode* current,prev,next;
prev=NULL;
current=head;
while(current!=NULL){
next=current->next;
current->next=prev;
prev=current;
current=next;
}
head=prev;
return head;
}
};
对我来说一切都很好,但我遇到了错误没有可行的重载\'=\'下一个=当前->下一个
我很想知道,为什么会出现这个错误?亲切地寻求帮助
-
next不是指针。 stackoverflow.com/a/3280765/920069 -
在
ListNode* current,prev,next;中,只有current是指向ListNode的指针,其余都是ListNode类型。使用ListNode *current, *prev, *next;。 -
接近一个错字。
ListNode* current,prev,next;与ListNode* current; ListNode prev; ListNode next;相同。你想要ListNode* current,*prev,*next;
标签: c++ data-structures linked-list