LinkedList - Notes By ShariqSP
LinkedList in Java
LinkedList
is a doubly linked list implementation of the List
interface in Java. It provides efficient insertion and deletion operations at both ends of the list and supports sequential access to elements.
Definition:
A LinkedList
is a collection that consists of a sequence of elements, where each element is connected to its previous and next elements via pointers, forming a chain of nodes.
Methods:
- add(E element): Adds the specified element to the end of the list.
- add(int index, E element): Inserts the specified element at the specified position in the list.
- remove(int index): Removes the element at the specified position in the list.
- get(int index): Returns the element at the specified position in the list.
- size(): Returns the number of elements in the list.
- isEmpty(): Returns true if the list contains no elements.
- contains(Object obj): Returns true if the list contains the specified element.
- clear(): Removes all elements from the list.
Example:
import java.util.LinkedList;
public class Main {
public static void main(String[] args) {
// Create a LinkedList of strings
LinkedList list = new LinkedList<>();
// Add elements to the list
list.add("Java");
list.add("Python");
list.add("C++");
// Display the elements
System.out.println("Elements in the list:");
System.out.println(list);
// Remove an element
list.remove("Python");
// Display the modified list
System.out.println("\nElements after removing 'Python':");
System.out.println(list);
}
}
In this example, a LinkedList
is created to store strings. Elements are added to the list using the add()
method and removed using the remove()
method.
Advantages:
- Dynamic Size: LinkedList provides dynamic sizing, allowing for the addition and removal of elements at runtime without needing to specify the size beforehand. This flexibility makes LinkedList suitable for scenarios where the size of the collection may vary.
- Fast Insertion and Deletion: LinkedList offers fast insertion and deletion operations, especially at the beginning or end of the list. Unlike ArrayList, where elements may need to be shifted during insertion and deletion, LinkedList simply adjusts its pointers, resulting in faster operations for frequent changes to the list.
- Memory Efficiency: LinkedList uses less memory compared to ArrayList for storing elements. While ArrayList requires additional memory for its internal array structure, LinkedList only requires memory for storing the elements themselves and the pointers linking them, resulting in lower memory overhead.
- Iterating over Elements: LinkedList provides efficient iteration over elements using iterators. Unlike ArrayList, where accessing elements by index may incur overhead for calculating offsets, LinkedList's node-based structure allows for efficient traversal and iteration.
Disadvantages:
- Random Access: LinkedList does not provide efficient random access to elements by index. While elements can be accessed by iterating over the list, accessing elements by index requires traversing the list from the beginning or end, resulting in slower performance compared to ArrayList for random access.
- Memory Overhead: While LinkedList may use less memory per element compared to ArrayList, it requires additional memory for storing pointers to the next and previous elements. This overhead can become significant, especially for large datasets or when the list contains a large number of small elements.
- Sequential Access: While LinkedList provides efficient insertion and deletion at the beginning or end of the list, operations that require sequential access to elements, such as searching for an element in the middle of the list, may be slower compared to ArrayList. ArrayList may be more efficient for scenarios that involve frequent access to elements by index.
- Not Thread-safe: LinkedList is not thread-safe by default. If multiple threads access a LinkedList concurrently, it can lead to data corruption or undefined behavior. Synchronization mechanisms need to be applied externally to ensure thread safety if needed, increasing complexity and potentially impacting performance.
Interview Questions and MCQs on ArrayList in Java
Interview Questions:
- What is an ArrayList in Java?
- How is ArrayList different from an array?
- What are the key features of ArrayList?
- How is the capacity of an ArrayList increased dynamically?
- What is the difference between ArrayList and LinkedList?
- How do you add elements to an ArrayList?
- How do you remove elements from an ArrayList?
- Explain the concept of resizing in ArrayList.
- What is the role of the ensureCapacity() method in ArrayList?
- What are the advantages and disadvantages of using ArrayList?
Multiple Choice Questions (MCQs):
- Which package is required to use ArrayList in Java?
a) java.util
b) java.lang
c) java.util.collection
d) java.util.arraylist
Answer: a) java.util - Which method is used to add an element to the end of an ArrayList?
a) addLast()
b) add()
c) append()
d) addToEnd()
Answer: b) add() - What happens when an element is added to an ArrayList that has reached its capacity?
a) The ArrayList is resized automatically
b) An exception is thrown
c) The element is added at the end
d) None of the above
Answer: a) The ArrayList is resized automatically - Which method is used to remove an element from an ArrayList at a specific index?
a) remove(index)
b) removeElement(index)
c) delete(index)
d) removeAt(index)
Answer: a) remove(index) - Which interface does ArrayList implement?
a) List
b) Collection
c) Iterable
d) All of the above
Answer: d) All of the above