Amazon Interview Question for Applications Developers


Country: India
Interview Type: Written Test




Comment hidden because of low score. Click to expand.
0
of 0 vote

//ideone.com/iJ8lh

public Node reverserLL(Node *head) {
        Node n = head;
        Node prev = null;
        Node tmp;
 
        while(n->next!=null)
        {
                temp = n->next;
                n->next = prev;
                n->prev = tmp;
                prev = n;
                n = tmp;        
        }
        return n;
}

- am15851 August 25, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

So this will stop when n = last element, prev = prev element and when temp would have been NULL.

So wouldn't it stop too early? The last element would not get updated.

- Martin August 26, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

void reverse_double_ll(Node* root) {
  Node* prev = NULL;
  Node* middle = root;
  Node* next = root->next;
  while (midde) {
    middle->next = prev;
    middle->prev = next;

    prev = middle;
    middle = next;
    next = middle ? middle->next : NULL;
  }
}

- Martin August 26, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Arghh, I hate the formatting of a newline on this site...

void reverse_double_ll(Node* root) {
  Node* prev = NULL;
  Node* middle = root;
  Node* next = root->next;
  while (midde) {
    middle->next = prev;
    middle->prev = next;
    prev = middle;
    middle = next;
    next = middle ? middle->next : NULL;
  }
}

- Martin August 26, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

void reverse_doubly_linked_list(DLL *head){
           DLL *temp = NULL; DLL *newNode;
          while(head != NULL){
                    newNode = head;
                    head->next = temp;
                    temp->prev = head;
                    temp = head;
                    head = newNode;          
           }
           return temp;
     }

- Anonymous August 26, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

If this is a double linked list, then all what you need to do to reverse is to move the head to the last element and it will be reversed :)

- KGR August 27, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

But the next should be pointing to the previous and the previous to the next, you will have to change all the previous and next.

- Naveen Reddy Mandadi August 27, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

void reversedoublylinked(node ** head) {
    node *cur = head;
    node *tmp;
    while (cur) {
        tmp = cur->prev;
        cur->prev = cur->next;
        cur->next = tmp;
        if (cur->prev)
            cur = cur->prev;
        else
           break;
    }
    *head = cur;
}

- iwanna November 14, 2014 | Flag Reply


Add a Comment
Name:

Writing Code? Surround your code with {{{ and }}} to preserve whitespace.

Books

is a comprehensive book on getting a job at a top tech company, while focuses on dev interviews and does this for PMs.

Learn More

Videos

CareerCup's interview videos give you a real-life look at technical interviews. In these unscripted videos, watch how other candidates handle tough questions and how the interviewer thinks about their performance.

Learn More

Resume Review

Most engineers make critical mistakes on their resumes -- we can fix your resume with our custom resume review service. And, we use fellow engineers as our resume reviewers, so you can be sure that we "get" what you're saying.

Learn More

Mock Interviews

Our Mock Interviews will be conducted "in character" just like a real interview, and can focus on whatever topics you want. All our interviewers have worked for Microsoft, Google or Amazon, you know you'll get a true-to-life experience.

Learn More