Empresa engajada
reverse a linked list in linear time, with constrained memory, no second container allowed.
Sigiloso
private void reverseList(Node *current, Node *prev) { if (current) { Node *temp = current->next; current->next = prev; return reverseList(temp, current); } else { return temp; } } TESTS: === T1: Node *head = [1]->[2]->[3]->[4]->NULL reverseList(head, NULL); current = [1]->[2]->[3]->[4]->NULL prev = NULL temp = [2]->[3]->[4]->NULL current = [1]->NULL reverseList(temp, current) --- current = [2]->[3]->[4]->NULL prev = [1]->NULL temp = [3]->[4]->NULL current = [2]->[1]->NULL reverseList(temp, current) --- current = [3]->[4]->NULL prev = [2]->[1]->NULL temp = [4]->NULL current = [3]->[2]->[1]->NULL reverseList(temp, current) --- current = [4]->NULL prev = [3]->[2]->[1]->NULL temp = NULL current = [4]->[3]->[2]->[1] reverseList(temp, current) --- current = NULL temp = [4]->[3]->[2]->[1]->NULL RETURN temp === T2: Node *head = NULL; reverseList(head, NULL); RETURN NULL === T3: Node *head = [1]->NULL; reverseList(head, NULL); current = [1]->NULL prev = NULL temp = NULL current = [1]->NULL reverseList(temp, current) --- current = NULL temp = [1]->NULL RETURN temp ===