Question d’entretien chez Goldman Sachs

Reverse a linked-list.

Réponses aux questions d'entretien

Utilisateur anonyme

9 nov. 2014

User recursion, traverse the list until the element before the last. There do next.next = this, finish method. Recursion will make sure the list is reversed, while we roll back

1

Utilisateur anonyme

16 oct. 2014

Save the "next" pointer, save the "this" pointer as "previous", set the next pointer to NULL, follow the saved next pointer to the next item, then repeatedly save the next pointer, set next pointer to the saved "previous" pointer, until the next pointer is NULL (at which point we've reached the end of the list). We now have a list where all the next pointers are pointing to the previous element and the first next pointer is pointing to NULL; i.e. we've reversed the order of the list in one pass, with no need for extra data structures.

1

Utilisateur anonyme

13 mai 2011

As you traverse the list, add each node into a sorted data structure. When done, go through the data structure in reverse order and link them.

1

Utilisateur anonyme

25 juin 2011

make another list and add each element from the first list to the begining of the 2nd list

1