Rewrite the code- buid the linked list without dummy


Rewrite the code- buid the linked list without dummy node

#include
#include
#include
using namespace std;
struct Node {
int info;
Node* next;
};
void recycleList (Node * current) {
if(current != nullptr) {
recycleList (current->next);
delete (current);
}
}
int main () {
int n;
cout <<"How long a list do you want? "<cin>>n;
Node *head = new Node ();
head->info =0;
head->next = nullptr;
Node *current = head;
for (int i = 1; i <= n; i++) {
current->next = new Node ();
current->next->info =i;
current= current->next;
}
current->next = nullptr;
cout<<"Printing the list: "<current = head->next;
while(current != nullptr) {
cout<info <<' ';
current = current->next;
}
cout<recycleList(head);
return 0;
}

Solution Preview :

Prepared by a verified Expert
C/C++ Programming: Rewrite the code- buid the linked list without dummy
Reference No:- TGS01117518

Now Priced at $35 (50% Discount)

Recommended (96%)

Rated (4.8/5)