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:

Disadvantages:

Interview Questions and MCQs on ArrayList in Java

Interview Questions:

  1. What is an ArrayList in Java?
  2. How is ArrayList different from an array?
  3. What are the key features of ArrayList?
  4. How is the capacity of an ArrayList increased dynamically?
  5. What is the difference between ArrayList and LinkedList?
  6. How do you add elements to an ArrayList?
  7. How do you remove elements from an ArrayList?
  8. Explain the concept of resizing in ArrayList.
  9. What is the role of the ensureCapacity() method in ArrayList?
  10. What are the advantages and disadvantages of using ArrayList?

Multiple Choice Questions (MCQs):

  1. 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
  2. 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()
  3. 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
  4. 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)
  5. Which interface does ArrayList implement?
    a) List
    b) Collection
    c) Iterable
    d) All of the above
    Answer: d) All of the above