LinkedList in Java
Linked List is a part of the Collection framework present in java.util package. This class is an implementation of the LinkedList data structure which is a linear data structure where the elements are not stored in contiguous locations and every element is a separate object with a data part and address part. The elements are linked using pointers and addresses. Each element is known as a node. Due to the dynamicity and ease of insertions and deletions, they are preferred over the arrays. It also has a few disadvantages like the nodes cannot be accessed directly instead we need to start from the head and follow through the link to reach a node we wish to access. Show
How Does LinkedList work Internally? Since a LinkedList acts as a dynamic array and we do not have to specify the size while creating it, the size of the list automatically increases when we dynamically add and remove items. And also, the elements are not stored in a continuous fashion. Therefore, there is no need to increase the size. Internally, the LinkedList is implemented using the doubly linked list data structure. The main difference between a normal linked list and a doubly LinkedList is that a doubly linked list contains an extra pointer, typically called the previous pointer, together with the next pointer and data which are there in the singly linked list. Linked List Data Structure
Practice Problems on Linked List A linked list is a linear data structure, in which the elements are not stored at contiguous memory locations. The elements in a linked list are linked using pointers as shown in the below image: In simple words, a linked list consists of nodes where each node contains a data field and a reference(link) to the next node in the list. Topics :
Singly Linked List :
More >> Circular Linked List :
More >> Doubly Linked List :
More >> Misc :
Quick Links :
If you still need more assistance with your placement preparation, have a look at our Complete Interview Preparation Course. The course has been designed by our expert mentors to help students crack the coding interview of top product or service-based organizations . You get access to premium lectures, 200+ coding questions bank, resume building tips, and lifetime access to the course content. So to make sure that your next programming interview doesn’t feel like an interrogation, enroll in Complete Interview Preparation and give a boost to your placement preparation. Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above. Java LinkedList classJava LinkedList class uses a doubly linked list to store the elements. It provides a linked-list data structure. It inherits the AbstractList class and implements List and Deque interfaces. The important points about Java LinkedList are:
Hierarchy of LinkedList classAs shown in the above diagram, Java LinkedList class extends AbstractSequentialList class and implements List and Deque interfaces. Doubly Linked ListIn the case of a doubly linked list, we can add or remove elements from both sides. LinkedList class declarationLet's see the declaration for java.util.LinkedList class. Constructors of Java LinkedList
Methods of Java LinkedList
Java LinkedList ExampleOutput: Ravi Vijay Ravi Ajay Java LinkedList example to add elementsHere, we see different ways to add elements. Initial list of elements: [] After invoking add(E e) method: [Ravi, Vijay, Ajay] After invoking add(int index, E element) method: [Ravi, Gaurav, Vijay, Ajay] After invoking addAll(Collection<? extends E> c) method: [Ravi, Gaurav, Vijay, Ajay, Sonoo, Hanumat] After invoking addAll(int index, Collection<? extends E> c) method: [Ravi, John, Rahul, Gaurav, Vijay, Ajay, Sonoo, Hanumat] After invoking addFirst(E e) method: [Lokesh, Ravi, John, Rahul, Gaurav, Vijay, Ajay, Sonoo, Hanumat] After invoking addLast(E e) method: [Lokesh, Ravi, John, Rahul, Gaurav, Vijay, Ajay, Sonoo, Hanumat, Harsh] Java LinkedList example to remove elementsHere, we see different ways to remove an element. 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: [] Java LinkedList Example to reverse a list of elementsOutput: Ajay Vijay Ravi Java LinkedList Example: BookOutput: 101 Let us C Yashwant Kanetkar BPB 8 102 Data Communications & Networking Forouzan Mc Graw Hill 4 103 Operating System Galvin Wiley 6 Next TopicArrayList vs LinkedList ← prev next → Data Structures in Background- Java Collection Framework
1. OverviewIn this article, we are going to explore the most popular implementation of Map interface from the Java Collections Framework in more detail, picking up where our intro article left off. Before we get started with the implementation, it's important to point out that the primary List and Set collection interfaces extend Collection but Map does not. Simply put, the HashMap stores values by key and provides APIs for adding, retrieving and manipulating stored data in various ways. The implementation is based on the the principles of a hashtable, which sounds a little complex at first but is actually very easy to understand. Key-value pairs are stored in what is known as buckets which together make up what is called a table, which is actually an internal array. Once we know the key under which an object is stored or is to be stored, storage and retrieval operations occur in constant time, O(1) in a well-dimensioned hash map. To understand how hash maps work under the hood, one needs to understand the storage and retrieval mechanism employed by the HashMap. We'll focus a lot on these. Finally, HashMap related questions are quite common in interviews, so this is a solid way to either prepare an interview or prepare for it. |