Bloomberg interview question

How do you reverse a linked list

Interview Answers

Anonymous

11 Feb 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

Anonymous

7 Mar 2012

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

Anonymous

7 Dec 2012

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

Anonymous

21 Feb 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; }