Hello,
Still a beginner to this recursive thing. I'm having a really hard time trying to solve this problem. I'm trying to append the first node of a linked list to the end of that same list. One additional bonus is I'm only able to use a function that will take a single pointer (head).
The biggest problem I'm having, is that I know each time a function is invoked on the stack the local variables that are stored won't have their same values and will be initialized to their original values. The reason this presents a problem for me is I thought a viable solution would be to grab the first node and separate it from the original list, "traverse" to the end of the list, and then append once I reach the end. However, two problems arise.
The first problem: If my base case is when head->next is NULL, how can I know when to grab and store the first node, use the recursive call to do the proper appendage & know when that node is the only node in the list, still using this as my base case? Is there another base case I should be aware of?
Second: If I'm recursively going through the list, then how can I access the first node to move it once I'm at the end since I'm at the end of the list.
I then realized that this was a problem too. I'm stuck because I'm mostly confused. I can't find the logic in this problem. could somebody please explain to me a viable way to approach this problem?
Currently, my approach would be to:
1. Check if the list coming in is empty, exit if true.
2. Check if there is only one node in the list
a. If this is the one and only node, exit
b. If this is the end of the list, append
3. return to the rest of the program
//writing this question helped me to find another solution :) Point out if this would be a good recursive approach? Ideas, comments, open to all! Thank you!
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
|
//assume a list is passed in, its size is random (>= 0)
int append_first_to_last(node * & to_append)
{
node * tail = NULL;
if(!to_append)
return 0;
//set a pointer to the last node
if(!to_append->next){
tail = to_append;
return 1; //return to the invocation of this function
}
//traverse until the end of the list
if(to_append->next)
append_first_to_last(to_append->next);
//I'm stuck again, I can't find a solution past this point.
/* I know that I need to return to the very beginning of my list to append the node to tail
but I don't see how I can do that since I don't know where the beginning of the list is.
*/
This was an idea, but it was slowly proving wrong:
//once the tail pointer is set to the end, hop into the else condition
//this will append the first node to the last
//if we are at the one and only node, set the pointer to NULL and exit
/* check if there is more than one node in the list
before making the change to the last node */
/*
if(head->next)
{
node * first = head;
head = head->next;
first->next = NULL;
tail->next
}
}//end else condition
*/
}
|