|
| 1 | +# LinkedList in Java 📚 |
| 2 | +In Java, LinkedList is a class that implements the List interface and represents a linked list data structure. It is a linear data structure where each element, known as a node, contains a reference to the next node in the sequence, forming a chain-like structure. Unlike arrays, which have contiguous memory allocation, linked lists use dynamic memory allocation, which allows for efficient insertion and deletion operations, especially in the middle of the list. |
| 3 | + |
| 4 | +Here are some key points about LinkedList in Java: |
| 5 | + |
| 6 | +**Node Structure :** Each node in a LinkedList contains two fields: the data field, which holds the element value, and the next field, which holds a reference to the next node in the sequence. |
| 7 | + |
| 8 | +**Doubly Linked List :** In Java, the LinkedList class implements a doubly linked list, meaning each node contains references to both the next node and the previous node in the sequence. This allows for efficient traversal in both directions. |
| 9 | + |
| 10 | +**Dynamic Size :** Unlike arrays, linked lists do not have a fixed size. They can grow or shrink dynamically as elements are added or removed. |
| 11 | + |
| 12 | +**Random Access :** Unlike arrays, accessing elements in a linked list by index requires traversing the list from the beginning or end, which can be less efficient for large lists compared to arrays. |
| 13 | + |
| 14 | +**Insertion and Deletion :** Linked lists excel at insertion and deletion operations, especially in the middle of the list, as they only require adjusting references between nodes, rather than shifting elements like in arrays. |
| 15 | + |
| 16 | +Here's a simple example of how to use LinkedList in Java: |
| 17 | + |
| 18 | +```java |
| 19 | +import java.util.LinkedList; |
| 20 | + |
| 21 | +public class Main { |
| 22 | + public static void main(String[] args) { |
| 23 | + // Creating a LinkedList |
| 24 | + LinkedList<String> linkedList = new LinkedList<>(); |
| 25 | + |
| 26 | + // Adding elements |
| 27 | + linkedList.add("Apple"); |
| 28 | + linkedList.add("Banana"); |
| 29 | + linkedList.add("Orange"); |
| 30 | + |
| 31 | + // Printing elements |
| 32 | + System.out.println("LinkedList: " + linkedList); |
| 33 | + |
| 34 | + // Removing an element |
| 35 | + linkedList.remove("Banana"); |
| 36 | + System.out.println("LinkedList after removing 'Banana': " + linkedList); |
| 37 | + } |
| 38 | +} |
| 39 | +``` |
| 40 | +Output: |
| 41 | +```java |
| 42 | +LinkedList: [Apple, Banana, Orange] |
| 43 | +LinkedList after removing 'Banana': [Apple, Orange] |
| 44 | +``` |
| 45 | +## Iterating over a LinkedList |
| 46 | +LinkedList is a class in Java that provides a linked-list data structure. It implements the List interface and provides methods to manipulate the elements in the list. Iterating over a LinkedList is a common operation in Java programming. |
| 47 | +### Using Iterator |
| 48 | +```java |
| 49 | +import java.util.LinkedList; |
| 50 | +import java.util.Iterator; |
| 51 | + |
| 52 | +public class IterateLinkedList { |
| 53 | + public static void main(String[] args) { |
| 54 | + LinkedList<String> linkedList = new LinkedList<>(); |
| 55 | + linkedList.add("Apple"); |
| 56 | + linkedList.add("Banana"); |
| 57 | + linkedList.add("Orange"); |
| 58 | + |
| 59 | + // Using Iterator |
| 60 | + Iterator<String> iterator = linkedList.iterator(); |
| 61 | + while(iterator.hasNext()) { |
| 62 | + String element = iterator.next(); |
| 63 | + System.out.println(element); |
| 64 | + } |
| 65 | + } |
| 66 | +} |
| 67 | +``` |
| 68 | +### Using Enhanced for Loop |
| 69 | +```java |
| 70 | +import java.util.LinkedList; |
| 71 | + |
| 72 | +public class IterateLinkedList { |
| 73 | + public static void main(String[] args) { |
| 74 | + LinkedList<String> linkedList = new LinkedList<>(); |
| 75 | + linkedList.add("Apple"); |
| 76 | + linkedList.add("Banana"); |
| 77 | + linkedList.add("Orange"); |
| 78 | + |
| 79 | + // Using Enhanced for Loop |
| 80 | + for(String element : linkedList) { |
| 81 | + System.out.println(element); |
| 82 | + } |
| 83 | + } |
| 84 | +} |
| 85 | +``` |
| 86 | +### Using ListIterator |
| 87 | +```java |
| 88 | +import java.util.LinkedList; |
| 89 | +import java.util.ListIterator; |
| 90 | + |
| 91 | +public class IterateLinkedList { |
| 92 | + public static void main(String[] args) { |
| 93 | + LinkedList<String> linkedList = new LinkedList<>(); |
| 94 | + linkedList.add("Apple"); |
| 95 | + linkedList.add("Banana"); |
| 96 | + linkedList.add("Orange"); |
| 97 | + |
| 98 | + // Using ListIterator |
| 99 | + ListIterator<String> listIterator = linkedList.listIterator(); |
| 100 | + while(listIterator.hasNext()) { |
| 101 | + String element = listIterator.next(); |
| 102 | + System.out.println(element); |
| 103 | + } |
| 104 | + } |
| 105 | +} |
| 106 | +``` |
| 107 | +These are the common ways to iterate over a LinkedList in Java. Choose the method that suits your requirements best based on factors like performance, readability, and specific use case. |
| 108 | +## Sorting a LinkedList Using Collections.sort() |
| 109 | +Java's `Collections.sort()` method can be used to sort elements in a `LinkedList`. This method sorts the elements in their natural order or using a specified comparator. |
| 110 | +### Example 💡 |
| 111 | +```java |
| 112 | +import java.util.LinkedList; |
| 113 | +import java.util.Collections; |
| 114 | + |
| 115 | +public class Main { |
| 116 | + public static void main(String[] args) { |
| 117 | + LinkedList<Integer> linkedList = new LinkedList<>(); |
| 118 | + linkedList.add(5); |
| 119 | + linkedList.add(3); |
| 120 | + linkedList.add(8); |
| 121 | + linkedList.add(1); |
| 122 | + |
| 123 | + System.out.println("Original LinkedList: " + linkedList); |
| 124 | + |
| 125 | + // Sorting using Collections.sort() |
| 126 | + Collections.sort(linkedList); |
| 127 | + |
| 128 | + System.out.println("Sorted LinkedList: " + linkedList); |
| 129 | + } |
| 130 | +} |
| 131 | +``` |
0 commit comments