#include <bits/stdc .h>
using namespace std;
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) {}
};
void printList(ListNode *head) {
ListNode *curr = head;
while (curr != NULL) {
cout << curr->val;
curr = curr->next;
}
}
int main() {
ListNode *head[5];
ListNode *node;
head[0] = new ListNode(1,NULL);
for (int i = 1; i < 5; i ) {
head[i] = new ListNode(i 1, head[i - 1]);
}
node = head[5]; //cannot convert 'ListNode*' to 'ListNode**'
printList(node);
return 0;
}
How should i pass last node as single pointer to function ? i am not able to convert node in double pointer to single pointer variable.
CodePudding user response:
Like Armin pointed out in the comments, you are referencing the array beyond the range.
if you replace
node = head[5];
with
node = head[4]; //this is the 5th element of the array.
you would probably get the output you were expecting.