Pergunta de entrevista da empresa Amazon

Reverse a Linked List.

Resposta da entrevista

Sigiloso

17 de fev. de 2012

Pretty simple, just start at the head, iterate to the next, point next at prev until you hit the end. just need to manage 3 pointers. Some Dodgy C below. void reverseList(node **n) { node *prev = *n; if(prev == NULL) return; node *cur = prev ->next; if(cur == NULL) return; prev->next = NULL; /* New End of list */ do { node *tmp = cur ->next; cur->next = prev; prev = cur; cur = tmp; } while(cur != NULL); *n = prev; /* Reset the Head */ }