rewrite the code- buid the linked list without dummy node

profilew350500
link.pdf

#include <iostream> #include <iomanip> #include <string> 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? "<<endl; 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: "<<endl;

current = head->next; while(current != nullptr) {

cout<<current->info <<' '; current = current->next;

}

cout<<endl; recycleList(head);

return 0; }