【发布时间】:2022-10-14 19:11:32
【问题描述】:
/**
* 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* reverse(ListNode *prev,ListNode* curr,ListNode* future,ListNode *temp)
{
if(future==NULL)
{
*temp=*curr;
return NULL;
}
reverse(curr,curr->next,future->next,temp);
future->next=curr;
cout<<future->val<<" "<<curr->val;
return temp;
}
ListNode* reverseList(ListNode* head) {
ListNode* temp=new ListNode(0);
if(head==NULL)
{
return NULL;
}
return reverse(NULL,head,head->next,temp);
}
};
【问题讨论】:
-
这是因为代码没有意义。
-
关于成为精英 C++ 大师的秘密捷径有一个流行的神话:扔掉你的 C++ 教科书;而是做随机编码谜题,否则它们没有内在的学习价值,除了不良的编程习惯外,它们不会教任何东西。这个神话来自许多点击诱饵网站,它们承诺做他们的谜题会将任何人变成即时的 C++ 超级黑客。每个人最终都意识到这些编码难题是多么无用,但只有在浪费大量时间做一个又一个编码难题之后。他们没有什么可证明的。
标签: c++ recursion linked-list singly-linked-list function-definition