YouTip LogoYouTip

Java Linkedlist Add

[![Image 1: Java LinkedList](#) Java LinkedList](#) * * * The LinkedList class provides multiple overloaded versions of the add() method for adding elements to the list. ### 1. boolean add(E e) This is the most basic add method, appending the specified element to the end of the list. ## Instance LinkedList list =new LinkedList(); list.add("Apple"); list.add("Banana"); list.add("Cherry"); **Method Features**: * Always returns true (since LinkedList allows duplicate elements) * Time complexity is O(1) * Equivalent to the addLast() method ### 2. void add(int index, E element) Inserts the specified element at the specified position in the list. ## Instance LinkedList list =new LinkedList(); list.add("Apple"); list.add("Banana"); list.add(1, "Orange");// Insert at index 1 **Method Features**: * If the index equals the list size, it is equivalent to add(E e) * Requires traversal to the specified position, average time complexity is O(n) * May throw IndexOutOfBoundsException ### 3. boolean addAll(Collection c) Appends all elements in the specified collection to the end of the list, in the order they are returned by the collection's iterator. ## Instance LinkedList fruits =new LinkedList(); fruits.add("Apple"); fruits.add("Banana"); List moreFruits =Arrays.asList("Orange", "Grape"); fruits.addAll(moreFruits); **Method Features**: * Returns true if the list changes as a result of the call * Time complexity is O(m), where m is the size of the collection argument ### 4. boolean addAll(int index, Collection c) Inserts all elements in the specified collection into this list, starting at the specified position. ## Instance LinkedList fruits =new LinkedList(); fruits.add("Apple"); fruits.add("Banana"); List moreFruits =Arrays.asList("Orange", "Grape"); fruits.addAll(1, moreFruits);// Insert collection at index 1 **Method Features**: * Requires traversal to the specified position first * Time complexity is O(n + m), where n is the list size and m is the collection size * * * ## Comparison with Other Methods ### add() vs addLast() * `add(E e)` and `addLast(E e)` are completely equivalent * Both are used to add elements to the end of the list ### add() vs offer() * `add(E e)` may throw an exception in a capacity-constrained queue * `offer(E e)` returns false instead of throwing an exception in a capacity-constrained queue ### add() vs push() * `push(E e)` actually calls `addFirst(E e)` * Adds elements to the beginning of the list rather than the end * * * ## Performance Considerations ### Efficiency of Add Operations * **Add to end**: O(1) time complexity * **Add to beginning**: O(1) time complexity (using addFirst()) * **Add to middle**: O(1) time complexity (requires traversal to the specified position) ### Comparison with ArrayList | Operation | LinkedList | ArrayList | | --- | --- | --- | | Add to end | O(1) | O(1) amortized | | Add to beginning | O(1) | O(n) | | Add to middle | O(n) | O(n) | | Random access | O(n) | O(1) | * * * ## Practical Application Examples ### Creating a Todo List ## Instance LinkedList todoList =new LinkedList(); // Add tasks todoList.add("Buy groceries"); todoList.addFirst("Check emails");// High priority task todoList.add(1, "Call mom");// Medium priority System.out.println(todoList); // Output: [Check emails, Call mom, Buy groceries] ### Implementing a Simple Queue ## Instance LinkedList queue =new LinkedList(); // Enqueue operations queue.add("First"); queue.add("Second"); queue.add("Third"); // Dequeue operations while(!queue.isEmpty()){ System.out.println(queue.removeFirst()); } ### Merging Two Lists ## Instance LinkedList list1 =new LinkedList(Arrays.asList("A", "B", "C")); LinkedList list2 =new LinkedList(Arrays.asList("D", "E", "F")); list1.addAll(list2); System.out.println(list1);// Output: [A, B, C, D, E, F] * * * ## Notes 1. **Thread Safety**: LinkedList is not thread-safe, external synchronization is required in multi-threaded environments 2. **Null Elements**: Null elements are allowed to be added 3. **Index Checking**: Pay attention to boundary conditions when using add methods with index 4. **Concurrent Modification**: Modifying the list while iterating with an iterator will throw ConcurrentModificationException [![Image 2: Java LinkedList](#) Java LinkedList](#)
← Java Linkedlist AddfirstJava Vector Trimtosize β†’