InterviewSolution
| 1. |
What is a LinkedList? Is it a linear or non-linear data structure? What are the advantages LinkedList has over an ArrayList? |
|
Answer» A LINKED list is a data structure in which elements are linked to each other using pointers. Each element or node consists of a data field and reference to the next node in the list. Unlike an ArrayList, the elements are not stored in contiguous memory locations. The entry point to the linked list is called head and has the reference to the first node in the list. The last node in the list has reference to null. A linked list is a linear data structure. A linear data structure is ONE in which data is arranged in an orderly fashion with the elements attached adjacently to each other. A non-linear data structure is one in which elements are arranged in a hierarchical fashion, creating a relationship among the elements. Some of the examples of the nonlinear data structure are tree and graph. An ArrayList is essentially an array with elements placed in contiguous memory locations. This makes it much faster when doing get (search) and set (update) operations. HOWEVER, inserts and deletions are much faster in a LinkedList is no resizing of the list needs to take place as well as no COPYING of content to a new list is needed in case the PREVIOUS one gets full. In addition, the linked list is dynamic in nature and memory is allocated when required. |
|