Amazon.com Interview Question
1,572 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 at Amazon.com:
Merge two sorted linked lists with unique integers.
See more for this Amazon.com Software Development Engineer Interview
Helpful Question?
Yes |
No
Inappropriate?
Answers & Comments (2)
2 of 2 people found this helpful
if (list1.head == null && list2.head == null) {
System.out.println("Empty list"); //checks if list is empty
}
if (list1.head == null) {
list2.printList();
}
if (list2.head == null) {
list1.printList();
}
LinkList list3 = new LinkList();
Node a = list1.head;
Node b = list2.head;
while (a != null && b != null) {
if (a.value < b.value) {
list3.insert(a.value);
a = a.next;
} else if (a.value > b.value) {
list3.insert(b.value);
b = b.next;
} else if (a.value == b.value){ //inserts only unique value to the merged list
list3.insert(a.value);
a = a.next;
b = b.next;
}
}
if (a == null) {
while (b != null) {
list3.insert(b.value);
b = b.next;
}
}
if (b == null) {
while (a != null) {
list3.insert(a.value);
a = a.next;
}
}
list3.printList();
}
Helpful Answer?
Yes |
No
Inappropriate?
To comment on this
question,
Sign In with Facebook or
Sign Up



0 of 0 people found this helpful
by Interview Candidate: