Amazon Interview Question for Software Engineer / Developers






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

Recursively using 1 extra variable:

void Reverse(node *&head)
{
	if(head==NULL)
		return;
	node *fwd=head->next;
	if(fwd==NULL)
		return;
	 else
	 {
	 	     Reverse(fwd);
	 }
	 head->next->next=head;
	 head->next=NULL;
	 head=fwd;

}

- Anubhav Singh June 16, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

The recursive method is possible, but for me, it is hard to follow.

Iterative one is easy.

void reverse(node **head) {
if (!*head) return;

node *cur = *head;
node *prev = NULL;
node *next = NULL;

while (cur) {
next = cur->next;
cur->next = prev;

prev = cur;
cur = next;
}

*head = prev;

return;
}

- nunbit romance June 14, 2007 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

there is one more boundary condition. what if there's only one node? in that case your first statement after the while(cur) will throw null pointer exception.

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

sorry, my mistake. your code is correct.

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

void reverse(Node *ptr, Node *prev)
{
      if(ptr->next == NULL)
      {
             ptr->next = prev;
             return ptr;
      }
     else
     {
          Node *temp = reverse(ptr->next, ptr);
          ptr->next =prev;
          return temp;
      }

- chandra August 09, 2012 | 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