Java LinkedList
Explore a list that also works as a deque.
முக்கிய விளக்கங்கள் தமிழில் உள்ளன. Runnable example மற்றும் கூடுதல் practice notes ஆங்கிலத்தில் உள்ளன.
இந்தப் பாடத்தில்…
- Explain the key idea behind LinkedList.
- Read and run a complete example, then predict what changes.
- Recognize the topic-specific mistake discussed below.
அடிப்படையைப் புரிந்துகொள்ளுங்கள்
LinkedList implements both List and Deque. End operations such as addFirst and removeFirst make its behavior easy to explore. It is not automatically faster than ArrayList for every insert or delete; traversal, memory use, and locality also matter.
விரிவான விளக்கங்கள்
Java LinkedList என்னன்னு தெரியுமா?
LinkedList-உம் ArrayList மாதிரியே ஆனா internally different-ஆ work ஆகும். ஒவ்வொரு element-உம் next element-ஐ point பண்றது.
LinkedList use பண்றதுக்கு:
ArrayList vs LinkedList
- நிறைய data store பண்றதுக்கு ArrayList better
- Frequent add/remove operations-க்கு LinkedList better
- Random access (index-ல) ArrayList faster
- Sequential access LinkedList efficient
LinkedList<String> cars = new LinkedList<>();
cars.add("Volvo");
cars.add("BMW");
cars.add("Ford");
cars.addFirst("Mazda");
cars.addLast("Toyota");
cars.removeFirst();
cars.removeLast();
System.out.println(cars.getFirst()); // Volvo
System.out.println(cars.getLast()); // Ford
System.out.println(cars); // [Volvo, BMW, Ford]`LinkedList` எப்போது `ArrayList`-ஐ விட சிறந்தது?
- Random access (index) operations-க்கு
- Frequent insertions/deletions-க்கு, குறிப்பாக list-ன் நடுவில்
- Memory குறைவாக use செய்ய
- Sorting-க்கு
Reveal answer / விடை
Frequent insertions/deletions-க்கு, குறிப்பாக list-ன் நடுவில்
`LinkedList` middle insertions/deletions O(1) (node pointer change மட்டும்). `ArrayList` middle insert O(n) (shift required). ஆனால் random access-க்கு ArrayList O(1), LinkedList O(n).
முழுமையான உதாரணம்
This complete Main.java program focuses on the lesson idea. Reference fragments above may demonstrate individual statements; this example includes the imports, entry point, and supporting types it needs.
import java.util.LinkedList;
public class Main {
public static void main(String[] args) throws Exception {
LinkedList<String> queue = new LinkedList<>();
queue.addLast("Kavi");
queue.addLast("Malar");
queue.addFirst("Arun");
System.out.println(queue.removeFirst());
System.out.println(queue);
}
}Predict first. Then reveal the expected output / வெளியீடு
Arun [Kavi, Malar]
The output shown was checked by running this complete example. Trace the executed statements in order, then compare with your run.
Change it. Run it. Understand it.
Load this lesson example into the editor. Change one value or statement, predict the result, then press RUN to execute Java.
Powered by OneCompiler. Code you run is sent to OneCompiler for execution. For Scanner examples, supply input in the editor I/O panel; for file examples, files exist in its execution environment.
நீங்களே முயற்சி செய்யுங்கள்
Practice challenge
Use peekFirst before removeFirst and compare the list contents afterwards.
Reveal the answer and reasoning / விடை
peekFirst reads without removing; removeFirst reads and removes. For a general queue or stack, also consider ArrayDeque.
தவிர்க்க வேண்டிய தவறு: get(index) on LinkedList requires traversal. Do not assume constant-time random access.
பாடத்தின் சுருக்கம்
- Explain the example without reading the output first.
- Make the change requested in the practice task.
- Describe why the result changed—or why compilation failed.
Ready to move on? Follow the next lesson in course order, or return to the curriculum to review a prerequisite.