Pergunta de entrevista da empresa Google

Reverse a linked list in place

Respostas da entrevista

Sigiloso

30 de ago. de 2010

Pseudocode reverselist(head): currhead=head; next1=curr->next; while(next1!=null): tmp=next1->next; next1->next=currhead; currhead=next1; next1=tmp return currhead;

Sigiloso

30 de ago. de 2010

You forgot to make NULL at the end of new list. You should add currhead->next=null before the loop.

Sigiloso

8 de out. de 2010

#include "stdafx.h" #include #include struct LNode { char val; LNode* next; }; LNode* make_list(const std::string& s) { LNode* root = 0; LNode* last = 0; for(std::string::const_iterator it=s.begin();it!=s.end();++it) { LNode* n = new LNode(); n->val = *it; if(!root) { root = n; } else last->next = n; last = n; } return root; } LNode* reverse(LNode* node, LNode* next = 0) { if(!node->next)// last node { node->next = next; return node; } LNode* reversed = reverse(node->next, node); node->next = next; return reversed; } LNode* reverse1(LNode* root) { LNode* front = 0; LNode* cur = root; LNode* nxtcur = 0; while(cur) { nxtcur = cur->next; cur->next = front; front = cur; cur = nxtcur; } return front; } int _tmain(int argc, _TCHAR* argv[]) { LNode* node = make_list("test list"); node = reverse(node); while(node) { std::coutval; node=node->next; } std::coutval; node=node->next; } std::cout<

Sigiloso

27 de ago. de 2010

How did you get the result of you interview? Did they call you or mail you? How much time it takes for the result to come?