← Back to Core Java Course | Chapter 11: Collections | Lesson 5 of 17

Java का LinkedList

एक LinkedList items की एक chain है जहां हर एक अगले की ओर point करता है, clues वाले एक treasure hunt जैसा। बीच में items जोड़ना या हटाना तेज़ है।
Syntax
java
LinkedList<Type> list = new LinkedList<>();
list.add(item);
list.addFirst(item);
list.removeLast();

Introduction to LinkedList

LinkedList एक contiguous array के बजाय nodes की एक doubly-linked chain उपयोग करके List implement करता है, इसलिए हर element को सिर्फ अपने neighbors जानने की ज़रूरत है, एक fixed memory block नहीं।

उदाहरण: Introduction to LinkedList

java
import java.util.LinkedList;
public class Main {
	public static void main(String[] args) {
		LinkedList<String> list = new LinkedList<>(); // chain of nodes
		list.add("a");
		list.add("b");
		System.out.println(list);
	}
}

LinkedList as a Queue / Deque

चूंकि यह Deque भी implement करता है, LinkedList सीधे एक queue (FIFO, offer()/poll() से) या एक stack (LIFO, push()/pop() से) के रूप में उपयोग किया जा सकता है बिना एक अलग data structure की ज़रूरत के।

उदाहरण: LinkedList as a Queue / Deque

java
import java.util.LinkedList;
public class Main {
	public static void main(String[] args) {
		LinkedList<Integer> queue = new LinkedList<>();
		queue.offer(1); // FIFO
		queue.offer(2);
		System.out.println(queue.poll());
		LinkedList<Integer> stack = new LinkedList<>();
		stack.push(1); // LIFO
		stack.push(2);
		System.out.println(stack.pop());
	}
}

Inserting and Removing Nodes

एक बार आपके पास उस position का reference हो, एक node insert या remove करना तेज़ है (O(1)), क्योंकि यह सिर्फ neighboring pointers relink करने की बात है — दूसरे elements shift करने की ज़रूरत नहीं।

उदाहरण: Inserting and Removing Nodes

java
import java.util.LinkedList;
public class Main {
	public static void main(String[] args) {
		LinkedList<String> list = new LinkedList<>();
		list.add("a");
		list.add("c");
		list.add(1, "b"); // fast insert once at that position
		System.out.println(list);
	}
}

Iterating over LinkedList

एक LinkedList को iterate करना syntactically एक ArrayList जैसा ही काम करता है (for-each या Iterator), लेकिन index से random access धीमा है (O(n)) क्योंकि list को एक दिए position तक पहुंचने के लिए एक end से node-by-node चलना पड़ता है।

उदाहरण: Iterating over LinkedList

java
import java.util.LinkedList;
public class Main {
	public static void main(String[] args) {
		LinkedList<String> list = new LinkedList<>();
		list.add("a");
		list.add("b");
		for (String item : list) { // same syntax as ArrayList
			System.out.println(item);
		}
	}
}

ArrayList vs LinkedList

ArrayList चुनें जब आप ज़्यादातर index से पढ़ें; LinkedList चुनें जब आप ज़्यादातर ends पर या बीच में एक iterator के ज़रिए insert/remove करें — अपने access pattern के लिए गलत चुनना performance को काफी नुकसान पहुंचा सकता है।

उदाहरण: ArrayList vs LinkedList

java
import java.util.ArrayList;
import java.util.LinkedList;
public class Main {
	public static void main(String[] args) {
		ArrayList<Integer> arrayList = new ArrayList<>(); // best for index reads
		LinkedList<Integer> linkedList = new LinkedList<>(); // best for insert/remove at ends
		arrayList.add(1);
		linkedList.addFirst(1);
		System.out.println(arrayList.get(0));
		System.out.println(linkedList.getFirst());
	}
}
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
आम गलतियां
  1. एक LinkedList पर एक loop में get(index) उपयोग करना, जो हर बार धीमा है (O(n))।
  2. Random access के लिए LinkedList उपयोग करना, जब ArrayList बेहतर है।
  3. एक empty list पर removeFirst() call करना, जो NoSuchElementException throw करता है।

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.