问题
I am trying to do a reverse linked list recursively using pointer to pointer but the problem is that in the second loop the script crash. Can you help me in order to solve my problem. This is my code :
void reverseNumber(Mynbr** start){
Mynbr *header;
Mynbr *current;
if ((*start)){
header = (*start);
current = (*start)->next;
if (current && current->next!= NULL)
{
reverseNumber(current->next);
header = current;
current->next->next = current;
current->next = NULL;
}
}
}
回答1:
The way should be followed is:
1) Divide the list in two parts - first node and rest of the linked list.
2) Call reverse for the rest of the linked list.
3) Link rest to first.
4) Fix head pointer
void reverseNumber(struct Mynbr** start)
{
struct Mynbr* head;
struct Mynbr* rest;
/* empty list */
if (*start == NULL)
return;
/* suppose head = {1, 2, 3}, rest = {2, 3} */
head = *start;
rest = head->next;
/* List has only one node */
if (rest == NULL)
return;
/* reverse the rest list and put the head element at the end */
reverseNumber(&rest);
head->next->next = head;
/* tricky step -- see the diagram */
head->next = NULL;
/* fix the head pointer */
*start = rest;
}
回答2:
try this
void reverseNumber(Mynbr **start){
Mynbr *header = *start;
if(!header) return;
Mynbr *current = header->next;
if(!current) return;
header->next = NULL;
Mynbr *new_head = current;
reverseNumber(&new_head);
current->next = header;
*start = new_head;
}
回答3:
You need to pass pointer to a pointer in the call of reverseNumber(current->next). It should be reverseNumber(&(current->next)). I think you are not returning the new head. Also, the reversed list will end prematurely. For example, if the list is 1->2->3->4->5->NULL then after reverse it will be 5-4->NULL.
回答4:
I don't understand why the double pointer. It servers no purpose. So, I have actually written a simple program doing what you need.
Assuming this will be the structure of your Mynbr
struct Mynbr {
int k;
struct Mynbr* next;
};
Type defining the structure
typedef struct Mynbr Mynbr_t;
My linked list reversing function would be like this (which is called recursively)
void reverseNumber(Mynbr_t* start) {
if (start == NULL) return;
static Mynbr_t* head;
Mynbr_t* current = start;
if (current->next != NULL) {
reverseNumber(current->next);
head->next = current;
head = current;
head->next = NULL;
} else
head = current;
}
Go ahead, test it, using this below code. It simply reverses the list.
int main() {
size_t Mynbr_size = sizeof(Mynbr_t);
Mynbr_t* start = (Mynbr_t*) malloc(Mynbr_size);
Mynbr_t* current = start;
int i;
for (i=0; i<10; i++) {
current->k = i;
if (i!=9) {
current->next = (Mynbr_t*) malloc(Mynbr_size);
current = current->next;
}
}
current = start;
Mynbr_t* last = NULL;
while (current != NULL) {
printf("%d\n", current->k);
current = current->next;
if (current != NULL)
last = current; // you need to grab this to loop through reverse order
}
reverseNumber(start);
current = last;
while (current != NULL) {
printf("%d\n", current->k);
current = current->next;
}
current = last;
Mynbr_t* temp;
while (current->next != NULL) {
temp = current;
current = current->next;
free(temp); // always free the allocated memory
} last = NULL;
return 0;
}
来源:https://stackoverflow.com/questions/37725393/reverse-linked-list-recursively