The simplest way is to use C++'s own linked lists. Since you have nullptr, you have std::forward_list (otherwise you can use std::list with no changes to the program)
In general, you delete dynamically-allocated objects by creating classes that manage these objects, like forward_list does in this example. Its destructor walks the list with an equivalent of your while(p->next) p=p->next and calls delete on each node.
(btw, to print the first input, change line 33 to cout << p->data << "\n"; You should also protect your program from non-numeric or empty input, which is where it crashes on p->next: mine doesn't)