Amazon.com

www.amazon.com

Interview Question

Software Development Engineer Intern Interview Seattle, WA

Reverse a linked list - both iterative and recursive way.

Answer

Interview Answer

2 Answers

0

Node reverse(Node head) {

    Node temp;
    Node previous = NULL;
    while (head != NULL) {
         temp = head.next;
         head.next = previous;
         previous = head;
         head = temp;
    }
    return previous;
}

Anon on Dec 4, 2013
0

void reverse(Node* & curr)
{
    if(!curr)
        return;

    Node* p=curr->next;
        if(!p)
            return;

    reverse(p);
    curr->next->next=curr;
    curr->next=NULL;
    curr=p;
}

anonymous on Jan 9, 2014

Add Answers or Comments

To comment on this, Sign In or Sign Up.