Amazon.com Interview Question
1,213 Interview Reviews |
Back to all Amazon.com Interview Questions & Reviews
Interview questions and reviews posted anonymously by interview candidates
Interview Question for Software Development Engineer II at Amazon.com:
How would you reverse a linked list in Java?
See more for this Amazon.com Software Development Engineer II Interview
Helpful Question?
Yes |
No
Inappropriate?
Answers & Comments (4)
2 of 2 people found this helpful
Helpful Answer?
Yes |
No
Inappropriate?
0 of 1 people found this helpful
Helpful Answer?
Yes |
No
Inappropriate?
1 of 2 people found this helpful
private E _element;
private LinkedListElement<E> _next;
public LinkedListElement(E element)
{
_element = element;
_next = null;
}
public LinkedListElement(E element, LinkedListElement<E> next)
{
_element = element;
_next = next;
}
public E get()
{
return _element;
}
public LinkedListElement<E> next()
{
return _next;
}
public void setNext(LinkedListElement<E> next)
{
_next = next;
}
public static <E> LinkedListElement<E> reverse(LinkedListElement<E> e)
{
if (e == null) {
return null;
}
LinkedListElement<E> current = e;
LinkedListElement<E> next = e.next();
e.setNext(null);
while (next != null) {
LinkedListElement<E> forward = next.next();
next.setNext(e);
e = next;
next = forward;
}
return e;
}
}
Helpful Answer?
Yes |
No
Inappropriate?
Members can
answer or comment on this question
–
Join Now (It's Free) or
Sign In



3 of 9 people found this helpful
by Interview Candidate: