##在OJ中做题
###题目描述
输入一个链表,从尾到头打印链表每个节点的值。
我的代码:
// struct ListNode {
// int val;
// struct ListNode *next;
// ListNode(int x) :
// val(x), next(NULL) {
// }
// };
class Solution {
public:
vector<int> printListFromTailToHead(struct ListNode* head) {
struct ListNode* p1, p2;
p1 = head;
p1->next = NULL;
p2 = p1->next, p2->next = p1;
p1 = p2;
while(p1->next != NULL) {
p2 = p1->next;
p2->next = p1;
p1 = p2;
}
//现在p1指向新链表头
vector<int> out;
int i = 0;
while(p1 != NULL) {
out[i++] = p1->val;
p1 = p1->next;
}
}
};
我想先反转链表再顺序输出,可是结构体变量声明那一行提示没有构造函数错误(去掉结构体定义的注释号提示结构体重复定义,所以我认为结构体在后台已经定义过了)
错误信息:
编译错误:您提交的代码无法完成编译
In file included from main.cc:2:
./solution.h:13:30: error: no matching constructor for initialization of 'struct ListNode'
struct ListNode* p1, p2;
^
../../include/tools.h:115:2: note: candidate constructor not viable: requires single argument 'x', but no arguments were provided
ListNode(int x) :
^
../../include/tools.h:112:8: note: candidate constructor (the implicit copy constructor) not viable: requires 1 argument, but 0 were provided
struct ListNode {
^
../../include/tools.h:112:8: note: candidate constructor (the implicit move constructor) not viable: requires 1 argument, but 0 were provided
1 error generated.
提交运行