Linked list Exercises Java
Similar to arrays in Java, LinkedList is a linear data structure. However LinkedList elements are not stored in contiguous locations like arrays, they are linked with each other using pointers. Each element of the LinkedList has the reference(address/pointer) to the next element of the LinkedList. Show Table of Contents1. LinkedList Representation LinkedList representationEach element in the LinkedList is called the Node. Each Node of the LinkedList contains two items: 1) Content of the element 2) Pointer/Address/Reference to the Next Node in the LinkedList. This is how a LinkedList looks: Note: Why do we need a Linked List?You must be aware of the arrays which is also a linear data structure but arrays have certain limitations such as: 2) Array elements need contiguous memory locations to store their values. 3) Inserting an element in an array is performance wise expensive as we have to shift several elements to make a space for the new element. For example: Similarly deleting an element from the array is also a performance wise expensive operation because all the elements after the deleted element have to be shifted left. These limitations are handled in the Linked List by providing following features: 2. Linked list elements dont need contiguous memory locations because elements are linked with each other using the reference part of the node that contains the address of the next node of the list. 3. Insert and delete operations in the Linked list are not performance wise expensive because adding and deleting an element from the linked list doest require element shifting, only the pointer of the previous and the next node requires change. Hierarchy of LinkedList class in JavaJava Linked List example of adding elementsIn the following example we are using add(), addFirst() and addLast() methods to add the elements at the desired locations in the LinkedList, there are several such useful methods in the LinkedList class which I have mentioned at the end of this article. package com.beginnersbook; import java.util.*; public class JavaExample{ public static void main(String args[]){ LinkedListOutput: Java example of removing elements from the LinkedListIn the following example we are checking out the few popular remove methods in the LinkedList that are used to remove elements from certain positions in the LinkedList. Detailed explanation of these methods along with examples are covered in the separate tutorials, links are provided at the end of this article. Output: Example of LinkedList in Javaimport java.util.*; public class LinkedListExample { public static void main(String args[]) { /* Linked List Declaration */ LinkedListOutput: Linked List Content: [Item1, Item5, Item3, Item6, Item2] LinkedList Content after addition: [First Item, Item1, Item5, Item3, Item6, Item2, Last Item] First element: First Item First element after update by set method: Changed first item LinkedList after deletion of first and last element: [Item1, Item5, Item3, Item6, Item2] Final Content: [Newly added item, Item1, Item3, Item6, Item2]Methods of LinkedList class:
For all the examples in the below methods, considerllistobj as a reference for LinkedList LinkedList 1) boolean add(Object item): It adds the item at the end of the list. llistobj.add("Hello");It would add the string Hello at the end of the linked list. 2) void add(int index, Object item): It adds an item at the given index of the the list. llistobj.add(2, "bye");This will add the string bye at the 3rd position( 2 index is 3rd position as index starts with 0). 3) boolean addAll(Collection c): It adds all the elements of the specified collection c to the list. It throws NullPointerException if the specified collection is null. Consider the below example LinkedListThis piece of code would add all the elements of ArrayList to the LinkedList. 4) boolean addAll(int index, Collection c): It adds all the elements of collection c to the list starting from a give index in the list. It throws NullPointerException if the collection c is null and IndexOutOfBoundsException when the specified index is out of the range. llistobj.add(5, arraylist);It would add all the elements of the ArrayList to the LinkedList starting from position 6 (index 5). 5) void addFirst(Object item): It adds the item (or element) at the first position in the list. llistobj.addFirst("text");It would add the string text at the beginning of the list. 6) void addLast(Object item): It inserts the specified item at the end of the list. This statement will add a string Chaitanya at the end position of the linked list. 7) void clear(): It removes all the elements of a list. llistobj.clear();8) Object clone(): It returns the copy of the list. For e.g. My linkedList has four items: text1, text2, text3 and text4. Object str= llistobj.clone(); System.out.println(str);Output: The output of above code would be: [text1, text2, text3, text4] 9) boolean contains(Object item): It checks whether the given item is present in the list or not. If the item is present then it returns true else false. boolean var = llistobj.contains("TestString");It will check whether the string TestString exist in the list or not. 10) Object get(int index): It returns the item of the specified index from the list. Object var = llistobj.get(2);It will fetch the 3rd item from the list. 11) Object getFirst(): It fetches the first item from the list. Object var =llistobj.getFirst();12) Object getLast(): It fetches the last item from the list. Object var= llistobj.getLast();13) int indexOf(Object item): It returns the index of the specified item. llistobj.indexOf("bye");14) int lastIndexOf(Object item): It returns the index of last occurrence of the specified element. int pos = llistobj.lastIndexOf("hello);integer variable pos will be having the index of last occurrence of string hello. 15) Object poll(): It returns and removes the first item of the list. 16) Object pollFirst(): same as poll() method. Removes the first item of the list. Object o = llistobj.pollFirst();17) Object pollLast(): It returns and removes the last element of the list. Object o = llistobj.pollLast();18) Object remove(): It removes the first element of the list. llistobj.remove();19) Object remove(int index): It removes the item from the list which is present at the specified index. llistobj.remove(4);It will remove the 5th element from the list. 20) Object remove(Object obj): It removes the specified object from the list. llistobj.remove("Test Item");21) Object removeFirst(): It removes the first item from the list. llistobj.removeFirst();22) Object removeLast(): It removes the last item of the list. llistobj.removeLast();23) Object removeFirstOccurrence(Object item): It removes the first occurrence of the specified item. llistobj.removeFirstOccurrence("text");It will remove the first occurrence of the string text from the list. 24) ObjectremoveLastOccurrence(Object item): It removes the last occurrence of the given element. llistobj.removeLastOccurrence("String1);It will remove the last occurrence of string String1. 25) Object set(int index, Object item): It updates the item of specified index with the give value. llistobj.set(2, "Test");It will update the 3rd element with the string Test. 26) int size(): It returns the number of elements of the list. llistobj.size();LinkedList TutorialsHere are the tutorials that I have shared on LinkedList. LinkedList Basics
Add/Remove
Get/Search
Iterator/ListIterator
Other Tutorials
Conversion
Differences
Reference
|