πŸŽ‰ Festival Deal: Save Big on Professional Courses with Real Projects – Apply Coupons FEST300OFF FEST500OFF
β†’ Click Here ←

Working of LinkedList in Java  


Steps of Working of LinkedList:
  • Below are the steps of working of LinkedList in Java:
    1. Creation of LinkedList:
      • A new LinkedList can be created using:
        LinkedList<String> list = new LinkedList<>();
      • Initially, the LinkedList is empty β€” it has no nodes. The head and tail both point to null.
      • Internal structure at the beginning: Head β†’ null, Tail β†’ null, Size = 0
    2. Adding the First Element:
      • When we add the first element using add():
        list.add("A");
      • A new Node is created that contains:
        • Data β†’ "A"
        • Previous β†’ null
        • Next β†’ null
        • Size β†’ 0
      • Both head and tail point to this single node.
        Working of LinkedList after adding first element
    3. Adding More Elements:
      • When we add more elements, new nodes are created and connected sequentially.
        list.add("B");
        list.add("C");
        list.add("D");
      • Each new node is linked at the end of the list:
        Working of LinkedList after adding first element
      • The Previous pointer of each node points to the previous element, and the Next pointer points to the next element β€” making it a Doubly Linked List.
    4. Adding or Deleting Elements in the Middle:
      • When inserting or deleting (removing) elements in the middle, the surrounding nodes’ pointers are adjusted.
      • Example – inserting "X" between "B" and "C":
        Working of LinkedList after adding first element
      • The Previous and Next references of neighboring nodes are updated accordingly.
    5. Traversal of LinkedList:
      • The LinkedList is traversed sequentially from head to tail using Iterator or ListIterator.
      • Example:
        Iterator<String> itr = list.iterator();
        while (itr.hasNext())
        {
            System.out.println(itr.next());
        }
      • Unlike ArrayList, elements are not accessed using indexes directly; instead, traversal follows node links.

LinkedList is Good for:
  1. Insertion and Deletion at the beginning, middle or end:
    • Each element (called a node) contains a reference to the previous and next node.
    • Insertion and deletion only involve changing the links, not shifting elements like in ArrayList.
  2. Frequent modifications (add/remove):
    • Performs efficiently when elements are frequently added or removed from different positions.
  3. Memory management of variable-size data:
    • Each node is allocated separately in memory, allowing dynamic growth without resizing.
LinkedList is Not Good for:
  1. Accessing elements by index (get(int index)):
    • Slow because traversal starts from the head or tail node to reach the given index (no direct access).
  2. Memory overhead:
    • Each node stores extra references (next and prev), consuming more memory.
  3. Poor cache locality:
    • Nodes are scattered in memory (non-contiguous), resulting in slower iteration compared to ArrayList.
  4. Random access operations:
    • Since nodes are connected by references, random access takes O(n) time.
Summary Table:
Operation Performance Complexity
Access by index (get) Slow O(n)
Update by index (set) Slow O(n)
Add at beginning/end Fast O(1)
Insert in middle Fast (if reference known) O(1) for known node, otherwise O(n)
Remove by node Fast (if reference known) O(1) for known node, otherwise O(n)
Search (contains) Slow O(n)
Iteration Slower O(n), not cache-friendly