How to use LinkedList in Java

Java LinkedList

 
Java LinkedList is linear Java data structures where all the elements are stored in non-contiguous memory locations. A doubly linked list is used in Java LinkedList. 
 

Features of Java LinkedList 

  1. It inherits the AbstractList class and implements List and Deque interfaces.
  2. It can contain duplicate elements.
  3. It can maintain insertion order.
  4. It is non-synchronized
  5. Manipulation is fast in this because no shifting needs to occur
  6. It can be used as a list, stack, or queue.
  7. The elements are linked using pointers and addresses.
  8. Each element is known as a node.
  9. It is part of java.util

Shortcomings of Java LinkedList

  1. Random access is not possible
  2. We need to start from the head and follow through the link to reach a node we wish to access
  3. It uses more money than arrays because of the storage used by their pointers.
  4. Memory os wasted in allocating space for a back-pointer

LinkedList Class Declaration from official Oracle Doc

 
  1. public class LinkedList <E> extends AbstarctSequential<E> implements List<E>, Deque<E>, Cloneable, Serializable  

Java LinkedList Constructors 

  1. LinkedList()  

    It is used to construct an empty list.
    1. import java.util.LinkedList;  
    2.   
    3. class Csharpcorner              
    4. {              
    5.     public static void main(String[] args)          
    6.     {              
    7.         LinkedList l1 = new LinkedList();  
    8.     }                
    9. }     
  2. LinkedList( Collection <? extends E> c)

     It constructs a list containing the elements of the specified collection, in order, they are returned by the collection's iterator.
    1. import static java.lang.System.out;  
    2. import java.util.ArrayList;  
    3. import java.util.LinkedList;  
    4.   
    5. class Csharpcorner              
    6. {              
    7.     public static void main(String[] args)          
    8.     {              
    9.         ArrayList<String> l1 = new ArrayList<String>();  
    10.         l1.add("c");  
    11.         l1.add("sahrp");  
    12.           
    13.         LinkedList<String> l2 = new LinkedList<String>(l1);  
    14.           
    15.         out.print(l2);  
    16.     }                
    17. }    
 

Java LinkedList Methods

 

1. LinkedList.add() 

 
Syntax 
 
1. boolean add(E a) 
 
It is used to append the specified element to the end of a list
 
2. void add(int index, E element)
 
It is used to insert the specified element at the specified position index in a list. 
  1. import static java.lang.System.out;  
  2. import java.util.ArrayList;  
  3. import java.util.LinkedList;  
  4.   
  5. class Csharpcorner              
  6. {              
  7.     public static void main(String[] args)          
  8.     {              
  9.         LinkedList<String> l1 = new Linked   List<String>();  
  10.         l1.add("c");  
  11.         l1.add("sharp");  
  12.         l1.add(2,"corner");  
  13.         
  14.         out.print(l1);  
  15.     }                
  16. }       
The above code, demonstrates both the versions of add methods 
 

2. LinkedList.addAll() 

 
Syntax 
 
1. boolean addAll( Collection<? extends E> c) 
 
It is used to append all of the elements in the specified collection to the end of this list, in the order that they are returned by the specified collection's iterator.
 
2. boolean addAll( int index, Collection<? extends E> c)
 
It is used to append all the elements in the specified collection, starting at the specified position of the list
  1. import static java.lang.System.out;  
  2. import java.util.LinkedList;  
  3.   
  4. class Csharpcorner              
  5. {              
  6.     public static void main(String[] args)          
  7.     {              
  8.         LinkedList<String> l1 = new LinkedList<String>();  
  9.         l1.add("c");  
  10.         l1.add("sharp");  
  11.         l1.add(2,"corner");  
  12.         
  13.         LinkedList<String> l2 = new LinkedList<String>();  
  14.         l2.addAll(l1);  
  15.           
  16.         l2.addAll(3, l2);  
  17.           
  18.         out.print(l2);  
  19.     }                
  20. }          
In the above code, we are demonstrating the use of both versions of addAll methods.
 

3. LinkedList.addFirst() 

 
Syntax
 
void addFirst(E e)
 
It is used to insert the given element at the beginning of a list.
  1. import static java.lang.System.out;  
  2. import java.util.LinkedList;  
  3.   
  4. class Csharpcorner              
  5. {              
  6.     public static void main(String[] args)          
  7.     {              
  8.         LinkedList<String> l1 = new LinkedList<String>();  
  9.         l1.add("c");  
  10.         l1.add("sharp");  
  11.         l1.add(2,"corner");  
  12.           
  13.         l1.addFirst(".com");  
  14.         out.print(l1);  
  15.     }                
  16. }        
The above code will shift the initial contents of the LinkedList and insert ".com" as the 1st element.  
 

4. LinkedList.addLast() 

 
Syntax 
 
void addLast(E e)
 
It is used to append the given element to the end of a list. 
  1. import static java.lang.System.out;  
  2. import java.util.LinkedList;  
  3.   
  4. class Csharpcorner              
  5. {              
  6.     public static void main(String[] args)          
  7.     {              
  8.         LinkedList<String> l1 = new LinkedList<String>();  
  9.         l1.add("c");  
  10.         l1.add("sharp");  
  11.         l1.add(2,"corner");  
  12.           
  13.         l1.addLast(".com");  
  14.         out.print(l1);  
  15.     }                
  16. }      
In the above code, we added ".com" as the last element of the LinkedList 
 

5. LinkedList.clear()

 
Syntax
 
void clear() 
 
It is used to remove all the elements from a list. 
  1. import static java.lang.System.out;  
  2. import java.util.LinkedList;  
  3.   
  4. class Csharpcorner              
  5. {              
  6.     public static void main(String[] args)          
  7.     {              
  8.         LinkedList<String> l1 = new LinkedList<String>();  
  9.         l1.add("c");  
  10.         l1.add("sharp");  
  11.         l1.add(2,"corner");  
  12.           
  13.         l1.clear();  
  14.         out.print(l1);  
  15.     }                
  16. }    
In the above code, we are removing all the elements of the LinkedList
 

6. LinkedList.clone() 

 
Syntax
 
Object clone() 
 
It is used to return a shallow copy of a LinkedList()
  1. import static java.lang.System.out;  
  2. import java.util.LinkedList;  
  3.   
  4. class Csharpcorner              
  5. {              
  6.     public static void main(String[] args)          
  7.     {              
  8.         LinkedList<String> l1 = new LinkedList<String>();  
  9.         l1.add("c");  
  10.         l1.add("sharp");  
  11.         l1.add(2,"corner");  
  12.         
  13.         LinkedList<String> l2 = new LinkedList<String>();  
  14.         l2 = (LinkedList<String>) l1.clone();  
  15.           
  16.         out.print(l2);  
  17.     }                
  18. }          
In the above code, we are making cloning l1 into l2
 

7. LinkedList.contains() 

 
Syntax
 
boolean contains(Object o) 
 
It is used to return true if a list contains a specified element.
  1. import static java.lang.System.out;  
  2. import java.util.LinkedList;  
  3.   
  4. class Csharpcorner              
  5. {              
  6.     public static void main(String[] args)          
  7.     {              
  8.         LinkedList<String> l1 = new LinkedList<String>();  
  9.         l1.add("c");  
  10.         l1.add("sharp");  
  11.         l1.add(2,"corner");  
  12.         
  13.         out.print(l1.contains("sharp"));  
  14.     }                
  15. }   
In the above code, we are checking if "sharp" is an element of LinkedList l1
 

8. LinkedList.containsAll() 

 
Syntax
 
boolean containsAll(Collection<?> E)
 
It is used to return true if a list contains all the elements.
  1. import static java.lang.System.out;  
  2. import java.util.LinkedList;  
  3.   
  4. class Csharpcorner              
  5. {              
  6.     public static void main(String[] args)          
  7.     {              
  8.         LinkedList<String> l1 = new LinkedList<String>();  
  9.         l1.add("c");  
  10.         l1.add("sharp");  
  11.         l1.add(2,"corner");  
  12.           
  13.         LinkedList<String> l2 = new LinkedList<String>(l1);   
  14.         
  15.         out.print(l1.containsAll(l2));  
  16.     }                
  17. }    
The above code checks if all the elements of LinkedList l2 are there in LinkedList l1
 

9. LinkedList.descendingIterator()

 
Syntax
 
Iterator<E> descendingIterator()
 
It returns an Iterator over the elements in this deque in reverse sequential order. 
  1. import static java.lang.System.out;  
  2. import java.util.Iterator;  
  3. import java.util.LinkedList;  
  4.   
  5. class Csharpcorner              
  6. {              
  7.     public static void main(String[] args)          
  8.     {              
  9.         LinkedList<String> l1 = new LinkedList<String>();  
  10.         l1.add("c");  
  11.         l1.add("sharp");  
  12.         l1.add(2,"corner");  
  13.           
  14.         Iterator x = l1.descendingIterator();  
  15.         
  16.         out.print(x);  
  17.     }                
  18. }        
The above code converts java,util.LinkedList to java.util.Spliterator. 
 

10. LinkedList.element()

 
Syntax 
 
E element() 
 
It is used to retrieve the first element of a list.
  1. import static java.lang.System.out;  
  2. import java.util.LinkedList;  
  3.   
  4. class Csharpcorner              
  5. {              
  6.     public static void main(String[] args)          
  7.     {              
  8.         LinkedList<String> l1 = new LinkedList<String>();  
  9.         l1.add("c");  
  10.         l1.add("sharp");  
  11.         l1.add(2,"corner");  
  12.           
  13.         String x = l1.element();  
  14.         
  15.         out.print(x);  
  16.     }                
  17. }     
The above will return the first element of the LinkedList l1
 

11. LinkedList.get()

 
Syntax
 
E get(int index)
 
It is used to return the element at the specified position in a list
  1. import static java.lang.System.out;  
  2. import java.util.LinkedList;  
  3.   
  4. class Csharpcorner              
  5. {              
  6.     public static void main(String[] args)          
  7.     {              
  8.         LinkedList<String> l1 = new LinkedList<String>();  
  9.         l1.add("c");  
  10.         l1.add("sharp");  
  11.         l1.add(2,"corner");  
  12.           
  13.         String x = l1.get(2);  
  14.         
  15.         out.print(x);  
  16.     }                
  17. }     
The above code will return the 3rd element from the LinkedList
 

12. LinkedList.getFirst()

 
Syntax 
 
E getFirst()
 
It is used to return the first element in a list
  1. import static java.lang.System.out;  
  2. import java.util.LinkedList;  
  3.   
  4. class Csharpcorner              
  5. {              
  6.     public static void main(String[] args)          
  7.     {              
  8.         LinkedList<String> l1 = new LinkedList<String>();  
  9.         l1.add("c");  
  10.         l1.add("sharp");  
  11.         l1.add(2,"corner");  
  12.           
  13.         String x = l1.getFirst();  
  14.         
  15.         out.print(x);  
  16.     }                
  17. }   
The above code will return the first element from the LinkedList
 

13. LinkedList.getLast()

 
Syntax 
 
E getLast() 
 
It is used to return the last element in a list
  1. import static java.lang.System.out;  
  2. import java.util.LinkedList;  
  3.   
  4. class Csharpcorner              
  5. {              
  6.     public static void main(String[] args)          
  7.     {              
  8.         LinkedList<String> l1 = new LinkedList<String>();  
  9.         l1.add("c");  
  10.         l1.add("sharp");  
  11.         l1.add(2,"corner");  
  12.           
  13.         String x = l1.getLast();  
  14.         
  15.         out.print(x);  
  16.     }                
  17. }   
The above code will return the last element from the LinkedList
 

14. LinkedList.indexOf()

 
Syntax
 
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.
  1. import static java.lang.System.out;  
  2. import java.util.LinkedList;  
  3.   
  4. class Csharpcorner              
  5. {              
  6.     public static void main(String[] args)          
  7.     {              
  8.         LinkedList<String> l1 = new LinkedList<String>();  
  9.         l1.add("c");  
  10.         l1.add("sharp");  
  11.         l1.add(2,"corner");  
  12.           
  13.         int x = l1.indexOf("c");  
  14.         
  15.         out.print(x);  
  16.     }                
  17. }     
The above code will return the first occurrence positional value of the given String value
 

15. LinkedList.lastIndexOf() 

 
Syntax 
 
int lastIndexOf(Object o) 
 
It returns the index of the last occurrence of the specified element in this list, or -1 if this list does not contain the element.
  1. import static java.lang.System.out;  
  2. import java.util.LinkedList;  
  3.   
  4. class Csharpcorner              
  5. {              
  6.     public static void main(String[] args)          
  7.     {              
  8.         LinkedList<String> l1 = new LinkedList<String>();  
  9.         l1.add("c");  
  10.         l1.add("sharp");  
  11.         l1.add(2,"corner");  
  12.           
  13.         int x = l1.lastIndexOf("c");  
  14.         
  15.         out.print(x);  
  16.     }                
  17. }   
The above code will return the last occurrence positional value of the given string.
 

16. LinkedList.listIterator()

 
Syntax
 
ListIterator<E> listIterator()
 
It returns a list-iterator of the elements in this list (in proper sequence), starting at the specified position in the list.
  1. import static java.lang.System.out;  
  2. import java.util.LinkedList;  
  3. import java.util.ListIterator;  
  4.   
  5. class Csharpcorner              
  6. {              
  7.     public static void main(String[] args)          
  8.     {              
  9.         LinkedList<String> l1 = new LinkedList<String>();  
  10.         l1.add("c");  
  11.         l1.add("sharp");  
  12.         l1.add(2,"corner");  
  13.           
  14.         ListIterator x = l1.listIterator(2);  
  15.         
  16.         out.print(x);  
  17.     }                
  18. }  
The above code will convert java.util.LinkedList to java.util.ListIterator
 

17. LinkedList.offer()

 
Syntax
 
boolean offer(E e)
 
It adds the specified element as the tail (last element) of this list 
  1. import static java.lang.System.out;  
  2. import java.util.LinkedList;  
  3.   
  4. class Csharpcorner              
  5. {              
  6.     public static void main(String[] args)          
  7.     {              
  8.         LinkedList<String> l1 = new LinkedList<String>();  
  9.         l1.add("c");  
  10.         l1.add("sharp");  
  11.         l1.add(2,"corner");  
  12.           
  13.         boolean x = l1.offer("c");  
  14.         
  15.         out.print(l1);  
  16.     }                
  17. }    
The above code will insert "c" at the tail of the LinkedList
 

18. LinkedList.offerFirst()

 
Syntax 
 
boolean offerFirst(E e )
 
It inserts the specified element at the front of this list. 
  1. import static java.lang.System.out;  
  2. import java.util.LinkedList;  
  3.   
  4. class Csharpcorner              
  5. {              
  6.     public static void main(String[] args)          
  7.     {              
  8.         LinkedList<String> l1 = new LinkedList<String>();  
  9.         l1.add("c");  
  10.         l1.add("sharp");  
  11.         l1.add(2,"corner");  
  12.           
  13.         boolean x = l1.offerFirst("c");  
  14.         
  15.         out.print(l1);  
  16.     }                
  17. }   
The above code will insert "c" at the starting of the LinkedList. 
 

19. LinkedList.offerLast()

 
Syntax 
 
boolean offerLast(E e) 
 
It inserts the specified element at the end of this list.  
  1. import static java.lang.System.out;  
  2. import java.util.LinkedList;  
  3.   
  4. class Csharpcorner              
  5. {              
  6.     public static void main(String[] args)          
  7.     {              
  8.         LinkedList<String> l1 = new LinkedList<String>();  
  9.         l1.add("c");  
  10.         l1.add("sharp");  
  11.         l1.add(2,"corner");  
  12.           
  13.         boolean x = l1.offerLast("c");  
  14.         
  15.         out.print(l1);  
  16.     }                
  17. }     
The above code will insert "c" at the end of the LinkedList.  
 

20. LinkedList.peek()

 
Syntax
 
 peek()
 
It retrieves but does not remove, the head (first element) of this list.
  1. import static java.lang.System.out;  
  2. import java.util.LinkedList;  
  3.   
  4. class Csharpcorner              
  5. {              
  6.     public static void main(String[] args)          
  7.     {              
  8.         LinkedList<String> l1 = new LinkedList<String>();  
  9.         l1.add("c");  
  10.         l1.add("sharp");  
  11.         l1.add(2,"corner");  
  12.           
  13.         String x = l1.peek();  
  14.         
  15.         out.print(l1);  
  16.     }                
  17. }        
The above code will return the first element from the LinkedList. When a stack is implemented using LinkedList then it returns the topmost element of the stack 
 

21. LinkedList.peekFirst() 

 
Syntax
 
E peekFirst()
 
It retrieves, but does not remove, the 1st element of this list, or returns null if this list is empty.
  1. import static java.lang.System.out;  
  2. import java.util.LinkedList;  
  3.   
  4. class Csharpcorner              
  5. {              
  6.     public static void main(String[] args)          
  7.     {              
  8.         LinkedList<String> l1 = new LinkedList<String>();  
  9.         l1.add("c");  
  10.         l1.add("sharp");  
  11.         l1.add(2,"corner");  
  12.           
  13.         String x = l1.peekFirst();  
  14.         
  15.         out.print(l1);  
  16.     }                
  17. }   
The above code returns the 1st element from the LinkedList
 

22. LinkedList.peekLast()

 
Syntax
 
E peekLast()
 
It retrieves, but does not remove, the last element of this list, or returns null if this list is empty
  1. import static java.lang.System.out;  
  2. import java.util.LinkedList;  
  3.   
  4. class Csharpcorner              
  5. {              
  6.     public static void main(String[] args)          
  7.     {              
  8.         LinkedList<String> l1 = new LinkedList<String>();  
  9.         l1.add("c");  
  10.         l1.add("sharp");  
  11.         l1.add(2,"corner");  
  12.           
  13.         String x = l1.peekLast();  
  14.         
  15.         out.print(l1);  
  16.     }                
  17. }   
The above code will return the last element of the LinkedList.
 

23. LinkedList.poll()

 
Syntax
 
E poll()
 
It retrieves and removes the head of this list
  1. import static java.lang.System.out;  
  2. import java.util.LinkedList;  
  3.   
  4. class Csharpcorner              
  5. {              
  6.     public static void main(String[] args)          
  7.     {              
  8.         LinkedList<String> l1 = new LinkedList<String>();  
  9.         l1.add("c");  
  10.         l1.add("sharp");  
  11.         l1.add(2,"corner");  
  12.           
  13.         String x = l1.poll();  
  14.         
  15.         out.print(l1);  
  16.     }                
  17. }      
The above code will remove "c" from the LinkedList
 

24. LinkedList.pollFirst()

 
Syntax
 
E pollFirst()
 
It retrieves and removes the head of this list or returns null if this list is empty
  1. import static java.lang.System.out;  
  2. import java.util.LinkedList;  
  3.   
  4. class Csharpcorner              
  5. {              
  6.     public static void main(String[] args)          
  7.     {              
  8.         LinkedList<String> l1 = new LinkedList<String>();  
  9.         l1.add("c");  
  10.         l1.add("sharp");  
  11.         l1.add(2,"corner");  
  12.           
  13.         String x = l1.pollFirst();  
  14.         
  15.         out.print(l1);  
  16.     }                
  17. }    
In the above code "c" will be returned and removed.
 

25. LinkedList.pollLast()

 
Syntax
 
E pollLast()
 
It retrieves and removes the tail of this list or returns null if this list is empty
  1. import static java.lang.System.out;  
  2. import java.util.LinkedList;  
  3.   
  4. class Csharpcorner              
  5. {              
  6.     public static void main(String[] args)          
  7.     {              
  8.         LinkedList<String> l1 = new LinkedList<String>();  
  9.         l1.add("c");  
  10.         l1.add("sharp");  
  11.         l1.add(2,"corner");  
  12.           
  13.         String x = l1.pollLast();  
  14.         
  15.         out.print(l1);  
  16.     }                
  17. }   
In the above code, "corner" will be returned and removed.
 

26. LinkedList.pop()

 
Syntax
 
E pop()
 
It pops an element from the stack represented by this List.
  1. import static java.lang.System.out;  
  2. import java.util.LinkedList;  
  3.   
  4. class Csharpcorner              
  5. {              
  6.     public static void main(String[] args)          
  7.     {              
  8.         LinkedList<String> l1 = new LinkedList<String>();  
  9.         l1.add("c");  
  10.         l1.add("sharp");  
  11.         l1.add(2,"corner");  
  12.           
  13.         String x = l1.pop();  
  14.         
  15.         out.print(l1);  
  16.     }                
  17. }      
The above code will pop "c" from the LinkedList
 

27. LinkedList.push()

 
Syntax
 
void push(E e)
 
It pushes an element onto the stack represented by this list.
  1. import static java.lang.System.out;  
  2. import java.util.LinkedList;  
  3.   
  4. class Csharpcorner              
  5. {              
  6.     public static void main(String[] args)          
  7.     {              
  8.         LinkedList<String> l1 = new LinkedList<String>();  
  9.         l1.add("c");  
  10.         l1.add("sharp");  
  11.         l1.add(2,"corner");  
  12.           
  13.         l1.push(".com");  
  14.         
  15.         out.print(l1);  
  16.     }                
  17. }    
The above code will push ".com" to the top of the stack
 

28. LinkedList.remove()

 
Syntax
 
1. remove()
 
It retrieves and removes the head of this list
 
2. remove(int index)
 
It removes the element at the specified position of this list.
 
3. remove(Object o)
 
It removes the first occurrence of the specified element from the list if it is present.
  1. import static java.lang.System.out;  
  2. import java.util.LinkedList;  
  3.   
  4. class Csharpcorner              
  5. {              
  6.     public static void main(String[] args)          
  7.     {              
  8.         LinkedList<String> l1 = new LinkedList<String>();  
  9.         l1.add("c");  
  10.         l1.add("sharp");  
  11.         l1.add(2,"corner");  
  12.           
  13.         l1.remove();  
  14.         out.print(l1);  
  15.           
  16.         l1.remove(0);  
  17.         out.print(l1);  
  18.           
  19.         l1.remove("corner");  
  20.         out.print(l1);  
  21.     }                
  22. }                       
The above code demonstrates all the versions of the remove method 
 

29. LinkedList.removeFirst()

 
Syntax
 
E removeFirst()
 
It removes and returns the first element from this list
  1. import static java.lang.System.out;  
  2. import java.util.LinkedList;  
  3.   
  4. class Csharpcorner              
  5. {              
  6.     public static void main(String[] args)          
  7.     {              
  8.         LinkedList<String> l1 = new LinkedList<String>();  
  9.         l1.add("c");  
  10.         l1.add("sharp");  
  11.         l1.add(2,"corner");  
  12.           
  13.         l1.removeFirst();  
  14.         out.print(l1);  
  15.     }                
  16. }  
The above code will remove and return the first element from the LinkedList.
 

30. LinkedList.removeFirstOccurence()

 
Syntax
 
boolean removeFirstOccurence(Object o) 
 
It removes the 1st occurrence of the specified element in this list (when traversing the list from head to tail)
  1. import static java.lang.System.out;  
  2. import java.util.LinkedList;  
  3.   
  4. class Csharpcorner              
  5. {              
  6.     public static void main(String[] args)          
  7.     {              
  8.         LinkedList<String> l1 = new LinkedList<String>();  
  9.         l1.add("c");  
  10.         l1.add("sharp");  
  11.         l1.add(2,"corner");  
  12.           
  13.         l1.removeFirstOccurrence("c");  
  14.         out.print(l1);  
  15.     }                
  16. }  
The above code will remove the first occurrence of "c"
 

31. LinkedList.removeLast()

 
Syntax
 
E removeLast()
 
It removes and returns the last element from the list
  1. import static java.lang.System.out;  
  2. import java.util.LinkedList;  
  3.   
  4. class Csharpcorner              
  5. {              
  6.     public static void main(String[] args)          
  7.     {              
  8.         LinkedList<String> l1 = new LinkedList<String>();  
  9.         l1.add("c");  
  10.         l1.add("sharp");  
  11.         l1.add(2,"corner");  
  12.           
  13.         String x = l1.removeLast();  
  14.         out.print(l1);  
  15.     }                
  16. }   
The above code will remove the last element of the LinkedList. 
 

32. LinkedList.removeLastOccurence()

 
Syntax
 
boolean removeLastOccurence(Object o)
 
It removes the last occurrence of the specified element in this list (when traversing the list from head to tail) 
  1. import static java.lang.System.out;  
  2. import java.util.LinkedList;  
  3.   
  4. class Csharpcorner              
  5. {              
  6.     public static void main(String[] args)          
  7.     {              
  8.         LinkedList<String> l1 = new LinkedList<String>();  
  9.         l1.add("c");  
  10.         l1.add("sharp");  
  11.         l1.add(2,"corner");  
  12.         l1.add("c");  
  13.           
  14.         l1.removeLastOccurrence("c");  
  15.         out.print(l1);  
  16.     }                
  17. }   
The above code will remove the last occurrence of the "c" from the LinkedList. 
 

33. LinkedList.set()

 
Syntax
 
E set(int index, E element)
 
It replaces the element at the specified position in this list with the specified element
  1. import static java.lang.System.out;  
  2. import java.util.LinkedList;  
  3.   
  4. class Csharpcorner              
  5. {              
  6.     public static void main(String[] args)          
  7.     {              
  8.         LinkedList<String> l1 = new LinkedList<String>();  
  9.         l1.add("c");  
  10.         l1.add("sharp");  
  11.         l1.add(2,"corner");  
  12.           
  13.         l1.set(1".com");  
  14.           
  15.         out.print(l1);  
  16.     }                
  17. }        
The above code will replace "sharp" with ".com"
 

34. LinkedList.size()

 
Syntax
 
int size()
 
It returns the number of elements in this list
  1. import static java.lang.System.out;  
  2. import java.util.LinkedList;  
  3.   
  4. class Csharpcorner              
  5. {              
  6.     public static void main(String[] args)          
  7.     {              
  8.         LinkedList<String> l1 = new LinkedList<String>();  
  9.         l1.add("c");  
  10.         l1.add("sharp");  
  11.         l1.add(2,"corner");  
  12.           
  13.         out.print(l1.size());  
  14.     }                
  15. }       
The above code will print the size of the LinkedList
 

35. LinkedList.toArray()

 
Syntax
 
1. Object [] toArray()
 
It returns an array containing all of the elements in this list in the proper sequence.
 
2. <T> T[] toArray(T[] a)
 
It returns an array containing all of the elements in this list in a proper sequence.  
  1. import static java.lang.System.out;  
  2. import java.util.LinkedList;  
  3.   
  4. class Csharpcorner              
  5. {              
  6.     public static void main(String[] args)          
  7.     {              
  8.         LinkedList<String> l1 = new LinkedList<String>();  
  9.         l1.add("c");  
  10.         l1.add("sharp");  
  11.         l1.add(2,"corner");  
  12.           
  13.         out.print(l1.toArray());  
  14.     }                
  15. }  
The above code will convert LinkedList type to Array type
 

Conclusion 

 
In the above article, we learned about Java LinkedList, features of Java LinkedList, Shortcomings of Java LinkedList, Java LinkedList constructors, Java LinkedList methods, and their Java implementation 


Similar Articles