InterviewSolution
| 1. |
When should you use an ArrayList and when a Linked List? How does a singly linked list differ from a doubly-linked list? |
|
Answer» An ArrayList is a collection of homogenous data types stored in contiguous memory locations while a LinkedList is a collection of data elements linked to each other using pointers. Since pointers are used the elements are not REQUIRED to occupy contiguous memory locations. Let’s see how this arrangement affects searching, insertion and DELETION of data.
Hence if the application involves a number of reads than writes an ArrayList is suitable otherwise a LinkedList. A singly LinkedList is one in which the node contains two fields – data – the actual content and – address – link to the next node. Though this type of list occupies less memory, it can be traversed in one direction only. A doubly LinkedList is one in which the node contains data and address of previous as well as the next node. This type of list can be traversed in both directions. |
|