Reputation: 81
I have a "pointer" which keeps incrementing and I need to return the "head" of the pointer finally. I am in a dilemma to use either "pointer[0]" or have another variable called "head" and initialize it and return at the end. The former I feel makes the code looks soiled and later costs little memory. Any clean suggestion?
Upvotes: 1
Views: 193
Reputation: 15164
You will always need two variables:
Foo *head = getSomeList();
Foo *tail = head;
while (tail->isValid())
++tail;
// You have head and tail here
You cannot really implement it much differently because tail[0] != head
(unless the list is empty).
Showing the code and telling us what exactly you try to achieve might lead to better answers.
Upvotes: 2