LinkedList in Java - Javatpoint
LinkedList in Java - Javatpoint
In the case of a doubly linked list, we can add or remove elements from both sides.
https://www.javatpoint.com/java-linkedlist 1/11
5/17/2021 LinkedList in Java - javatpoint
public class LinkedList<E> extends AbstractSequentialList<E> implements List<E>, Deque<E>, Cloneable, Serializable
Constructor Description
LinkedList(Collection<? It is used to construct a list containing the elements of the specified collection, in the order,
extends E> c) they are returned by the collection's iterator.
Method Description
boolean add(E e) It is used to append the specified element to the end of a list.
void add(int index, E element) It is used to insert the specified element at the specified position index in a list.
boolean addAll(Collection<? It is used to append all of the elements in the specified collection to the end of this list, in
extends E> c) the order that they are returned by the specified collection's iterator.
boolean addAll(Collection<? It is used to append all of the elements in the specified collection to the end of this list, in
extends E> c) the order that they are returned by the specified collection's iterator.
boolean addAll(int index, It is used to append all the elements in the specified collection, starting at the specified
Collection<? extends E> c) position of the list.
void addFirst(E e) It is used to insert the given element at the beginning of a list.
void addLast(E e) It is used to append the given element to the end of a list.
Iterator<E> It is used to return an iterator over the elements in a deque in reverse sequential order.
descendingIterator()
E get(int index) It is used to return the element at the specified position in a list.
int indexOf(Object o) It is used to return the index in a list of the first occurrence of the specified element, or -1
if the list does not contain any element.
int lastIndexOf(Object o) It is used to return the index in a list of the last occurrence of the specified element, or -1 if
the list does not contain any element.
https://www.javatpoint.com/java-linkedlist 2/11
5/17/2021 LinkedList in Java - javatpoint
ListIterator<E> listIterator(int It is used to return a list-iterator of the elements in proper sequence, starting at the
index) specified position in the list.
boolean offer(E e) It adds the specified element as the last element of a list.
E peekFirst() It retrieves the first element of a list or returns null if a list is empty.
E peekLast() It retrieves the last element of a list or returns null if a list is empty.
E pollFirst() It retrieves and removes the first element of a list, or returns null if a list is empty.
E pollLast() It retrieves and removes the last element of a list, or returns null if a list is empty.
E remove(int index) It is used to remove the element at the specified position in a list.
boolean remove(Object o) It is used to remove the first occurrence of the specified element in a list.
boolean It is used to remove the first occurrence of the specified element in a list (when traversing
removeFirstOccurrence(Object the list from head to tail).
o)
boolean It removes the last occurrence of the specified element in a list (when traversing the list
removeLastOccurrence(Object from head to tail).
o)
E set(int index, E element) It replaces the element at the specified position in a list with the specified element.
Object[] toArray() It is used to return an array containing all the elements in a list in proper sequence (from
first to the last element).
<T> T[] toArray(T[] a) It returns an array containing all the elements in the proper sequence (from first to the last
element); the runtime type of the returned array is that of the specified array.
https://www.javatpoint.com/java-linkedlist 3/11
5/17/2021 LinkedList in Java - javatpoint
import java.util.*;
al.add("Ravi");
al.add("Vijay");
al.add("Ravi");
al.add("Ajay");
Iterator<String> itr=al.iterator();
while(itr.hasNext()){
System.out.println(itr.next());
Output: Ravi
Vijay
Ravi
Ajay
import java.util.*;
ll.add("Ravi");
https://www.javatpoint.com/java-linkedlist 4/11
5/17/2021 LinkedList in Java - javatpoint
ll.add("Vijay");
ll.add("Ajay");
ll.add(1, "Gaurav");
ll2.add("Sonoo");
ll2.add("Hanumat");
ll.addAll(ll2);
ll3.add("John");
ll3.add("Rahul");
ll.addAll(1, ll3);
ll.addFirst("Lokesh");
ll.addLast("Harsh");
import java.util.*;
ll.add("Ravi");
ll.add("Vijay");
ll.add("Ajay");
ll.add("Anuj");
ll.add("Gaurav");
ll.add("Harsh");
ll.add("Virat");
ll.add("Gaurav");
ll.add("Harsh");
ll.add("Amit");
ll.remove("Vijay");
ll.remove(0);
ll2.add("Ravi");
ll2.add("Hanumat");
ll.addAll(ll2);
ll.removeAll(ll2);
ll.removeFirst();
ll.removeLast();
ll.removeFirstOccurrence("Gaurav");
ll.removeLastOccurrence("Harsh");
https://www.javatpoint.com/java-linkedlist 6/11
5/17/2021 LinkedList in Java - javatpoint
ll.clear();
Initial list of elements: [Ravi, Vijay, Ajay, Anuj, Gaurav, Harsh, Virat, Gaurav, Harsh, Amit]
After invoking remove(object) method: [Ravi, Ajay, Anuj, Gaurav, Harsh, Virat, Gaurav, Harsh, Amit]
After invoking remove(index) method: [Ajay, Anuj, Gaurav, Harsh, Virat, Gaurav, Harsh, Amit]
Updated list : [Ajay, Anuj, Gaurav, Harsh, Virat, Gaurav, Harsh, Amit, Ravi, Hanumat]
After invoking removeAll() method: [Ajay, Anuj, Gaurav, Harsh, Virat, Gaurav, Harsh, Amit]
After invoking removeFirst() method: [Gaurav, Harsh, Virat, Gaurav, Harsh, Amit]
After invoking removeLast() method: [Gaurav, Harsh, Virat, Gaurav, Harsh]
After invoking removeFirstOccurrence() method: [Harsh, Virat, Gaurav, Harsh]
After invoking removeLastOccurrence() method: [Harsh, Virat, Gaurav]
After invoking clear() method: []
import java.util.*;
ll.add("Ravi");
ll.add("Vijay");
ll.add("Ajay");
Iterator i=ll.descendingIterator();
while(i.hasNext())
System.out.println(i.next());
Output: Ajay
Vijay
Ravi
import java.util.*;
class Book {
https://www.javatpoint.com/java-linkedlist 7/11
5/17/2021 LinkedList in Java - javatpoint
int id;
String name,author,publisher;
int quantity;
public Book(int id, String name, String author, String publisher, int quantity) {
this.id = id;
this.name = name;
this.author = author;
this.publisher = publisher;
this.quantity = quantity;
//Creating Books
list.add(b1);
list.add(b2);
list.add(b3);
//Traversing list
for(Book b:list){
Output:
← Prev Next →
https://www.javatpoint.com/java-linkedlist 8/11
5/17/2021 LinkedList in Java - javatpoint
Preparation
Company
Interview
Questions
Company
Trending Technologies
https://www.javatpoint.com/java-linkedlist 9/11
5/17/2021 LinkedList in Java - javatpoint
B.Tech / MCA
https://www.javatpoint.com/java-linkedlist 10/11
5/17/2021 LinkedList in Java - javatpoint
https://www.javatpoint.com/java-linkedlist 11/11