-3
class Solution {
public:
    ListNode *detectCycle(ListNode *head) {
        
    }
};

I am learning C++. I don't have much experience with it so I don't know the term which I can google to get the meaning of the pointer in the function header. What is *detectCycle? What is the use for it? It can be a basic question but I really don't have any idea about it. Thanks.

  • 3
    If you start with a good book, tutorial, or online course, pointers should be explained along the way. – crashmstr Jul 16 '21 at 14:15
  • 1
    `detectCycle` is a function that returns a pointer to a ListNode. The exact purpose of it is not clear without any of the contents, but my guess is it traverses through a circular linked list and attempts to determine what node has a cycle, and returns a pointer to the address of it. – ollien Jul 16 '21 at 14:15
  • 2
    It may help to read it as `ListNode* detectCycle(ListNode *head)` instead. The `*` is associated with the return type, not the identifier. – François Andrieux Jul 16 '21 at 14:15
  • 3
    How are you learning? If you are following a [good book](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) this would have been covered by it. – StoryTeller - Unslander Monica Jul 16 '21 at 14:15
  • 1
    Just Google `c++ returning pointer from function` but before that undersatnd `c++ returning value from function`. – cse Jul 16 '21 at 14:17
  • @FrançoisAndrieux Thanks. That cleared my doubt. – Preet Kamal Jul 16 '21 at 14:23

1 Answers1

3

detectCycle is defined here to be a function that accepts a ListNode pointer (the * means pointer here) and returns a ListNode pointer.

Since the code block you have shown is empty, the function does nothing.

Since it declares a return value and does not return anything, using this function will result in undefined behavior.

Judging by the function name, the intent of the function is likely to be to determine whether the list has a cycle in it, i.e., a node of the list points back to a previous member of the list. The return value may be nullptr if there is no cycle, or a pointer to the node where a cycle was detected. This is speculation, however.

Fred Larson
  • 60,987
  • 18
  • 112
  • 174