Pergunta de entrevista da empresa Google

Describe how you would reverse a single linked list.

Respostas da entrevista

Sigiloso

21 de dez. de 2009

template class LList { public: T val; LList* next; public: static void Reverse(LList** head) { if(NULL == (*head)) return; LList* headNew = NULL; LList* cur = *head; LList* next; while(cur) { // to insert the pointer to the current node at the head of a new list next = cur->next; cur->next = headNew; headNew = cur; cur = next; } *head = headNew; } };

1

Sigiloso

24 de abr. de 2010

and same thing but recursive: reverse(head) { if(head.next==null) return head next = head.next newlist = reverse(next) next.next = head }