Open Dhanrajpatil123 opened 5 days ago
public class VariableDataTypeEx {
public static void main(String[] args) {
LinkedList<Integer> linkedList = new LinkedList<>();
linkedList.add(1);
linkedList.add(2);
linkedList.add(3);
linkedList.add(4);
linkedList.add(5);
System.out.println(linkedList.get(2)); // to search the value --> O(n)
linkedList.addFirst(10); // insertion --> O(1)
linkedList.getLast(); // get last --> O(1)
linkedList.remove(1);
linkedList.removeFirst();
linkedList.removeLast();
linkedList.removeIf(x -> x % 2 == 0); // output --> [1, 3, 5]
System.out.println(linkedList);
LinkedList<String> animals = new LinkedList<>(Arrays.asList("Cat", "Dog", "Tiger", "Elephant"));
LinkedList<String> animalsToRemove = new LinkedList<>(Arrays.asList("Dog", "Tiger", "Lion"));
animals.removeAll(animalsToRemove);
System.out.println(animals); // [Cat, Elephant]
}
LinkedList
LinkedList is a implementation class of List Interface
Inside a linked list we can store data in the form of Node like that -> | data | address | .
Node --> It is nothing but data + pointer
data -> here we can store actual data or we can say that value
address -> here we can store address of next value
Types of LinkedList
Now we talks about Java LinkedList
Performance Considerations
LinkedList has different performance characteristics compared to LinkedList :