Skip to content
C

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. What 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.

2. Why 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.

3. Real-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.

4. Syntax

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

5. Example 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]

6. Key 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.