Amazon.com
3.4 of 5 2,803 reviews
www.amazon.com Seattle, WA 5000+ Employees

Amazon.com Software Development Engineer Co-op Interview Question

I interviewed in Seattle, WA and was asked:
"How to print a link list reversely"
Tags: data structures
Add Tags [?]
Answer Flag Question

Part of a Software Development Engineer Co-op Interview Review - one of 4,146 Amazon.com Interview Reviews

Answers & Comments

2
of 2
votes
The simplest solution would be:

1. Traverse a linked list from head to tail
2. During traversal, push all the elements of the node into a stack
3. Once the traversal is done, pop all elements and this will print the linked list in reverse order ...

Looking fwd for an optimized solution ...
- Vikas on Jun 16, 2012 Flag Response
0
of 0
votes
void printLLreverse(Node *headNode)
{
    //this is going to just iterate through the LL
    //add each element to a stack and
    //print the stack when we are finished
    stack <int> llStack;
    Node* currNode = headNode;
    while(currNode)
    {
        llStack.push(currNode->data);
        currNode = currNode->next;
    }
    while(llStack.size() > 0)
    {
        cout << llStack.top();
        llStack.pop();
    }
}
- anonymous on Sep 25, 2012 Flag Response
0
of 0
votes
the stack solution is good, you can also use recursion
void printReverse(ListNode node) {
    if(node == null)
        return;
    printReverse(node.next);
    System.out.println(node.value);
}
- helloslz on Dec 29, 2013 Flag Response

To comment on this question, Sign In with Facebook or Sign Up


Tags are like keywords that help categorize interview questions that have something in common.

Glassdoor is your free inside look at Amazon.com interview questions and advice. All interview reviews posted anonymously by Amazon.com employees and interview candidates.