Skip to content
CampusEduX

Collections Framework · Lesson 75 of 150

LinkedList

LinkedList is another implementation of the List interface, where each element is stored as a node that also holds a reference (link) to the next (and previous) node, rather than sitting in one continuous block of memory like an array.

1 min read

01What is LinkedList?

LinkedList is another implementation of the List interface, where each element is stored as a node that also holds a reference (link) to the next (and previous) node, rather than sitting in one continuous block of memory like an array.

02Why is it used?

LinkedList is efficient for frequent insertions and deletions, especially at the beginning or middle of the list, since it only needs to update a few links rather than shifting many elements.

03Real-Life Example

Think of a treasure hunt where each clue tells you the location of the next clue. To reach any clue, you follow the chain from the start. LinkedList stores and accesses data through this kind of chain of connections.

04Syntax

java
LinkedList<DataType> listName = new LinkedList<>();

05Example Program

java
import java.util.LinkedList; public class LinkedListDemo { public static void main(String[] args) { LinkedList<String> names = new LinkedList<>(); names.add("Amit"); names.addFirst("Priya"); System.out.println(names); } }

Output:

[Priya, Amit]

06Key Points to Remember

  • LinkedList is efficient for insertions/deletions but slower for random access (like getting the 50th element) compared to ArrayList.
  • It also implements the Deque interface, so it can be used as a stack or a queue.
  • Choose ArrayList when access speed matters more; choose LinkedList when frequent insertions/deletions matter more.