java.util
package.
package java.util;
public class LinkedList<E> implements List<E>, Deque<E>, Cloneable, java.io.Serializable
{
// Constructors
// Methods
// Fields
}
LinkedList
implements both List
and Deque
interfaces, so it can be used as a list, queue, or stack.RandomAccess
, so accessing elements by index is slower compared to ArrayList
.LinkedList
Class
LinkedList
class:
Sr. No. | Constructor | Description |
---|---|---|
1 | LinkedList() |
Constructs an empty LinkedList. |
2 | LinkedList(Collection<? extends E> c) |
Constructs a LinkedList containing the elements of the specified collection, in the order returned by the collection’s iterator. |
LinkedList
Class
LinkedList
class:
Sr. No. | Method | Description |
---|---|---|
1 | void addFirst(E e) |
Inserts the specified element at the beginning of the LinkedList. |
2 | void addLast(E e) |
Appends the specified element to the end of the LinkedList. |
3 | E getFirst() |
Returns the first element in the LinkedList. |
4 | E getLast() |
Returns the last element in the LinkedList. |
5 | E removeFirst() |
Removes and returns the first element from the LinkedList. |
6 | E removeLast() |
Removes and returns the last element from the LinkedList. |
7 | void push(E e) |
Pushes an element onto the stack represented by this LinkedList. |
8 | E pop() |
Pops an element from the stack represented by this LinkedList. |
9 | boolean offer(E e) |
Inserts the specified element at the end of the queue. |
10 | E poll() |
Retrieves and removes the head (first element) of the queue, or returns null if the queue is empty. |
LinkedList
inherits all the methods of List
, Deque
, and Collection
interfaces.
LinkedList
class.
import java.util.LinkedList;
public class LinkedListDemo
{
public static void main(String[] args)
{
LinkedList<String> list = new LinkedList<>();
// Adding elements
list.add("Apple");
list.add("Banana");
list.add("Mango");
list.add("Banana"); // duplicate allowed
System.out.println(list);
System.out.println("-------------------------");
// Accessing first and last elements
System.out.println("First Element: " + list.getFirst());
System.out.println("Last Element: " + list.getLast());
System.out.println("-------------------------");
// Updating element
list.set(1, "Orange");
System.out.println(list);
System.out.println("-------------------------");
// Removing element
list.remove("Apple");
System.out.println(list);
System.out.println("-------------------------");
// Using LinkedList as a Queue
list.addFirst("Grapes");
list.addLast("Pineapple");
System.out.println(list);
System.out.println("-------------------------");
// Iterating LinkedList
for(String fruit : list)
{
System.out.println(fruit);
}
}
}
[Apple, Banana, Mango, Banana] ------------------------- First Element: Apple Last Element: Banana ------------------------- [Apple, Orange, Mango, Banana] ------------------------- [Orange, Mango, Banana] ------------------------- [Grapes, Orange, Mango, Banana, Pineapple] ------------------------- Grapes Orange Mango Banana Pineapple
LinkedList
Class:
List
and Deque
interfaces, so it can be used as a list, queue, or stack.Collections.sort()
or list.sort()
.ArrayList
, it does not guarantee data consistency in multi-threaded environments.
Your feedback helps us grow! If there's anything we can fix or improve, please let us know.
We’re here to make our tutorials better based on your thoughts and suggestions.