我正在尝试使用递归从头到尾打印链表中的每个节点。但是为什么我不能使用突出显示代码来实现递归?
class Solution {
public:
vector<int> printListFromTailToHead(ListNode* head) {
if(!head)
{
vector<int> a(0);
return a;
}
else if(!head -> next)
{
vector<int> a(1, head -> val);
return a;
}
else
/*
return printListFromTailToHead(head -> next).push_back(head -> val);
*/
}
};
答案 0 :(得分:2)
如果要打印,请先打印-不要构建矢量。
void printListFromTailToHead(ListNode* head)
{
if (head)
{
printListFromTailToHead(head->next);
std::cout << head->val << '\n';
}
}
如果您实际上不想想要打印任何东西但产生一个矢量,则您需要重新排列一下代码,因为push_back
不会返回任何内容:
vector<int> reverse_list(ListNode* head)
{
if (!head)
{
return vector<int>{};
}
else // You don't need a special case for a one-element list.
{
vector<int> ls = reverse_list(head->next);
ls.push_back(head->val);
return ls;
}
}
答案 1 :(得分:0)
查看此解决方案,也许不是更好的方法,但是您可以接受这个想法。事实是,您不需要返回数组,我也没有得到它为什么要返回向量,您可以再次调用该函数,然后像下面的代码那样打印它。
#include <iostream>
using namespace std;
class ListNode {
public:
ListNode *next;
int value;
ListNode() {
this->next = NULL;
this->value = 0;
}
ListNode(int _v) {
this->next = NULL;
this->value = _v;
}
};
void printListNodeFromTailToHead(ListNode *node) {
// If the current node is null will
// exit the recursive function
if (node == NULL) return ;
// Else will call the function again with next ListNode
// and print the current value
printListNodeFromTailToHead(node->next);
cout << node->value << endl;
}
int main() {
ListNode *a = new ListNode(1);
ListNode *tmp = a;
for (int i = 2; i < 10; i++) {
a->next = new ListNode(i);
a = a->next;
}
printListNodeFromTailToHead(tmp);
return 0;
}
答案 2 :(得分:0)
您的函数不输出任何内容。使用向量递归地输出列表是一个坏主意。
例如,该功能可以通过以下演示程序中的以下方式查看。
#include <iostream>
struct ListNode
{
int value;
ListNode *next;
};
void push_front( ListNode * &head, int value )
{
head = new ListNode { value, head };
}
std::ostream & printListFromHeadToTail( ListNode * &head, std::ostream &os = std::cout )
{
return head == nullptr ? os
: ( os << head->value << ' ', printListFromHeadToTail( head->next, os ) );
}
std::ostream & printListFromTailToHead( ListNode * &head, std::ostream &os = std::cout )
{
return head == nullptr ? os
: ( printListFromTailToHead( head->next, os ), os << head->value << ' ' );
}
int main()
{
const int N = 10;
ListNode *head = nullptr;
for ( int i = 0; i < N; i++ ) push_front( head, i );
printListFromHeadToTail( head ) << '\n';
printListFromTailToHead( head ) << '\n';
return 0;
}
其输出为
9 8 7 6 5 4 3 2 1 0
0 1 2 3 4 5 6 7 8 9
答案 3 :(得分:0)
我正在尝试使用以下命令从头到尾打印链接列表中的每个节点 递归
因为您没有提供有关列表的信息,所以在这里,我将使用“ std :: list”向您展示所需功能的简单实现。请注意,我提供的递归没有函数参数,没有全局范围变量,也没有静态变量(并且我使用cout,而不是print)。
此外,我使用称为Functor的类形式。我建议它们用于封装“小”功能。请查阅有关函子的文献...我认为它们比典型类“简单”。
#include <iostream>
using std::cout, std::cerr, std::endl, std::flush; // c++17
using std::ostream;
#include <list>
using std::list;
#include <string>
using std::string, std::to_string;
// typedefs are often simpler to read
// user typedefs ---------------vvvvvvvvvvv
typedef list<string> StrList_t;
typedef list<string>::iterator StrListIt_t;
// Functor
class F820_Recursive_t
{
// data attributes of class instance are not global nor static vars
StrList_t m_strList;
StrListIt_t m_it;
public:
int operator()() { return exec(); } // Functor entry
private:
int exec() // builds and modifies a std::list<string>
{
// example: using initializer list syntax
m_strList = StrList_t { "111", "222", "333", "444", "555", "666", "777", "888" };
recurseAndReportSize();
// example: appending to existing list
for (int i=301; i<309; ++i) {
string s = "S" + to_string(i);
m_strList.push_back (s);
}
recurseAndReportSize();
cout << "\n version cplusplus: " << __cplusplus << "\n\n" << endl;
return 0;
}
void recurseAndReportSize()
{
if (0 == m_strList.size()) {
cout << "\n empty list" << flush;
return;
}
cout << "\n\n recurse over list to count elements ... " << flush;
m_it = m_strList.begin(); // head to tail
uint count = recurseCount();
// report
cout << count << " elements" << endl;
// report list contents head to tail
m_it = m_strList.begin();
cout << "\n Head to Tail recursive content report "
<< "\n [" << coutListFromHeadToTail() << "]" << endl;
// report list contents tail to head
m_it = m_strList.end(); m_it--;
cout << "\n Tail to Head recursive content report: "
<< "\n [" << coutListFromTailToHead() << "]" << endl;
}
// recurse with no parameters, no static vars, no global vars
uint recurseCount( )
{ // --^-- no parameters
if (m_it == m_strList.end()) // RTC (recursion termination clause)
return 0;
m_it++; // step to next element
return (1 + recurseCount()); // tail recursion
}
// recurse with no parameters, no static vars, no global vars
string coutListFromHeadToTail ( )
{ // --^-- no parameters
cout << *m_it++;
if (m_it == m_strList.end()) // RTC (recursion termination clause)
return "";
cout << ", ";
return coutListFromHeadToTail(); // tail recursion
}
// recurse with no parameters, no static vars, no global vars
string coutListFromTailToHead ( )
{ // --^-- no parameters
if (m_it == m_strList.begin()) {
cout << *m_it;
return "";
}
cout << *m_it << ", ";
m_it--;
return coutListFromTailToHead(); // tail recursion
}
}; // class F820_Recursion_t
int main(int, char**) { return F820_Recursive_t()(); }
我的Lubuntu 19.04,g ++ v8.3上的输出
recurse over list to count elements ... 8 elements
Head to Tail recursive content report
[111, 222, 333, 444, 555, 666, 777, 888]
Tail to Head recursive content report:
[888, 777, 666, 555, 444, 333, 222, 111]
recurse over list to count elements ... 16 elements
Head to Tail recursive content report
[111, 222, 333, 444, 555, 666, 777, 888, S301, S302, S303, S304, S305, S306, S307, S308]
Tail to Head recursive content report:
[S308, S307, S306, S305, S304, S303, S302, S301, 888, 777, 666, 555, 444, 333, 222, 111]
version cplusplus: 201703