I'm new to C++ and in progress of learning it.
I am just wondering when should you define a class field to be a pointer, i.e. the "*next" in ListNode class? Is it possible to define the "next" to be an object? Why or why not?
Because you want the class field next to store a reference to a Node object. You don't want to store a copy of the Node object. All the Node objects are allocated on the heap, and the linked list is a data-structure that stores references to the various Nodes. Another way to think about it is this: you don't want your node object to store a node object inside of it that has a node object inside of it that... It won't be convenient this way.
pointer in c/c++ is the address referencing an object in the memory. and if you are using an object, it will be a copy of the object you want to reference, thus, you can not change the content of the original content, you can only change the copied object.
Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.