Pergunta de entrevista da empresa SingleStore

Reverse a linked list using recursion and otherwise.

Resposta da entrevista

Sigiloso

19 de fev. de 2017

The idea is that you recurs to the last node and keep the pre pointer at the same time. When you reach the last node change the current next pointer to the pre. ListNode* reverse (ListNode* head) { ListNode* p = head; return travel(p, nullptr, head); } ListNode* travel(ListNode* p, ListNode* pre) { (p->next == nullptr) { p->next = pre; return p; } ListNode* re = travel(p->next, p); p->next = pre; return re; }