Question d’entretien chez Bloomberg

How do you reverse a linked list

Réponses aux questions d'entretien

Utilisateur anonyme

11 févr. 2012

There are several ways you can reverse a linked list. One way would be to traverse through the list, each time removing the node you visit, and push the value of the node onto a stack. Once the list is empty, you pop the values one by one off the stack and append it onto the linked list until the stack is empty. The result is the same linked list only in reverse order. You could also append the values onto a separate list if you do so desire. If I missed anything, let me know. Happy problem solving!

1

Utilisateur anonyme

7 mars 2012

This video explains in detail how to reverse a linked list, hope it helps. http://www.youtube.com/watch?v=LgapVjJYxqc

Utilisateur anonyme

7 déc. 2012

I believe we can do a recursive traversing to create a new linked list

Utilisateur anonyme

21 févr. 2013

ll_node *reverse_recursive(ll_node *n) { // found the end of the linked list if(n->next == NULL) { first = n; return n; } ll_node *prev = reverse_recursive(n->next); prev->next = n; n->next = NULL; } void reverse_recursive_g() { ll_node *prev = reverse_recursive(first); last = prev; }