Why does this code cause no error
#include
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) {}
};
ListNode* removeNthFromEnd(ListNode* head, int n)
{
int count=0;
ListNode *temp, *temp2;
while(temp != NULL)
{
temp = temp->next;
count++;
}
return head;
}
void printList(ListNode* n)
{
while (n != NULL)
{
cout << n->val << " ";
n = n->next;
}
cout << "\n";
}
int main()
{
ListNode *head = new ListNode(1, new ListNode(2, new ListNode(3, new ListNode(4, new ListNode(5)))));
printList(head);
ListNode *head2 = removeNthFromEnd(head, 2);
printList(head2);
}
while this code does?
#include
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) {}
};
ListNode* removeNthFromEnd(ListNode* head, int n)
{
int count=0;
ListNode *temp, *temp2;
while(temp != NULL)
{
temp = temp->next;
count++;
}
return head;
}
void printList(ListNode* n)
{
while (n != NULL)
{
cout << n->val << " ";
n = n->next;
}
cout << "\n";
}
int main()
{
ListNode *head = new ListNode(1, new ListNode(2, new ListNode(3, new ListNode(4, new ListNode(5)))));
printList(head);
cout << "\n"; //THIS IS THE LINE THAT IS CAUSING THE ERROR
ListNode *head2 = removeNthFromEnd(head, 2);
printList(head2);
}
Only difference between both is that one line only