Explain how memory management and pointers work in C++, and write a function to safely reverse a singly linked list.