Java LinkedList

Linked list is a common basic data structure. It is a linear list but does not store data in a linear order. Instead, it stores the address of each node to the next node.

Linked lists can be divided into unidirectional lists and bidirectional lists.

A one-way linked list contains two values: the value of the current node and a link to the next node.

A bidirectional list has three integer values: the value, the backward node link, and the forward node link.

Java LinkedList is a common data container similar to ArrayList.

Compared with ArrayList, adding and deleting linkedLists is more efficient for operation, while finding and modifying is less efficient.

ArrayList is used when:

  • Frequent access to an element in the list.
  • You only need to add and remove elements at the end of the list.

Use LinkedList for the following situations:

  • You need to iterate through the loop to access certain elements in the list.
  • You need to frequently add and remove elements at the beginning, middle, and end of the list.

LinkedList descends from the AbstractSequentialList class.

LinkedList implements the Queue interface and can be used as a Queue.

LinkedList implements the List interface for list-related operations.

LinkedList implements the Deque interface and can be used as a queue.

LinkedList implements the Cloneable interface for cloning.

LinkedList implements the Java.io.Serializable interface, which supports serialization and can be transmitted via serialization.

The LinkedList class is in the java.util package and needs to be imported before use. The syntax is as follows:

Import java.util.LinkedList; // Import java.util. LinkedList<E> list = new LinkedList<E>(); List = new LinkedList(Collection<? extends E> c); // Create linked lists using collectionsCopy the code

Create a simple linked list instance:

// Introduce the LinkedList class

The instance

import java.util.LinkedList;

public class RunoobTest {
  public static void main(String[] args) {
    LinkedList<String> sites = new LinkedList<String>();
    sites.add("Google");
    sites.add("Runoob");
    sites.add("Taobao");
    sites.add("Weibo"); System.out.println(sites); }}Copy the code

In the above example, the output is as follows:

[Google, Runoob, Taobao, Weibo]
Copy the code

More often than not it is more efficient to use an ArrayList to access random elements in a list, but in several cases LinkedList provides a more efficient way.

Add an element at the beginning of the list:

The instance

// Introduce the LinkedList class
import java.util.LinkedList;

public class RunoobTest {
    public static void main(String[] args) {
        LinkedList<String> sites = new LinkedList<String>();
        sites.add("Google");
        sites.add("Runoob");
        sites.add("Taobao");
        // Use addFirst() to add elements to the header
        sites.addFirst("Wiki"); System.out.println(sites); }}Copy the code

In the above example, the output is as follows:

[Wiki, Google, Runoob, Taobao]
Copy the code

Add an element to the end of the list:

The instance

Import java.util.LinkedList; // Import java.util. public class RunoobTest { public static void main(String[] args) { LinkedList<String> sites = new LinkedList<String>(); sites.add("Google"); sites.add("Runoob"); sites.add("Taobao"); // Use addLast() to add the element sites.addlast ("Wiki") to the end; System.out.println(sites); }}Copy the code

In the above example, the output is as follows:

[Google, Runoob, Taobao, Wiki]
Copy the code

Remove an element at the beginning of the list:

The instance

// Introduce the LinkedList class
import java.util.LinkedList;

public class RunoobTest {
    public static void main(String[] args) {
        LinkedList<String> sites = new LinkedList<String>();
        sites.add("Google");
        sites.add("Runoob");
        sites.add("Taobao");
        sites.add("Weibo");
        // Remove the header element with removeFirst()sites.removeFirst(); System.out.println(sites); }}Copy the code

In the above example, the output is as follows:

[Runoob, Taobao, Weibo]
Copy the code

Remove an element from the end of the list:

The instance

// Introduce the LinkedList class
import java.util.LinkedList;

public class RunoobTest {
    public static void main(String[] args) {
        LinkedList<String> sites = new LinkedList<String>();
        sites.add("Google");
        sites.add("Runoob");
        sites.add("Taobao");
        sites.add("Weibo");
        // Use removeLast() to remove the tail elementsites.removeLast(); System.out.println(sites); }}Copy the code

In the above example, the output is as follows:

[Google, Runoob, Taobao]
Copy the code

Get the element at the beginning of the list:

The instance

// Introduce the LinkedList class
import java.util.LinkedList;

public class RunoobTest {
    public static void main(String[] args) {
        LinkedList<String> sites = new LinkedList<String>();
        sites.add("Google");
        sites.add("Runoob");
        sites.add("Taobao");
        sites.add("Weibo");
        // Use getFirst() to get the header elementSystem.out.println(sites.getFirst()); }}Copy the code

In the above example, the output is as follows:

Google
Copy the code

Get the element at the end of the list:

The instance

// Introduce the LinkedList class
import java.util.LinkedList;

public class RunoobTest {
    public static void main(String[] args) {
        LinkedList<String> sites = new LinkedList<String>();
        sites.add("Google");
        sites.add("Runoob");
        sites.add("Taobao");
        sites.add("Weibo");
        // Use getLast() to get the tail elementSystem.out.println(sites.getLast()); }}Copy the code

In the above example, the output is as follows:

Weibo
Copy the code

Iterative elements

We can use for with the size() method to iterate over elements in the list:

The instance

// Introduce the LinkedList class
import java.util.LinkedList;

public class RunoobTest {
    public static void main(String[] args) {
        LinkedList<String> sites = new LinkedList<String>();
        sites.add("Google");
        sites.add("Runoob");
        sites.add("Taobao");
        sites.add("Weibo");
        for (int size = sites.size(), i = 0; i < size; i++) { System.out.println(sites.get(i)); }}}Copy the code

The size() method is used to calculate the size of the linked list.

In the above example, the output is as follows:

Google
Runoob
Taobao
Weibo
Copy the code

You can also use for-each to iterate over elements:

The instance

// Introduce the LinkedList class
import java.util.LinkedList;

public class RunoobTest {
    public static void main(String[] args) {
        LinkedList<String> sites = new LinkedList<String>();
        sites.add("Google");
        sites.add("Runoob");
        sites.add("Taobao");
        sites.add("Weibo");
        for(String i : sites) { System.out.println(i); }}}Copy the code

In the above example, the output is as follows:

Google
Runoob
Taobao
Weibo
Copy the code

Commonly used method

methods describe
public boolean add(E e) Add an element to the end of the list and return success (true for success, false for failure).
public void add(int index, E element) Inserts an element to the specified location.
public boolean addAll(Collection c) Add all elements of a collection to the end of the list and return success, true for success, false for failure.
public boolean addAll(int index, Collection c) Adds all elements of a collection to the list after the specified location, returns success, true for success, false for failure.
public void addFirst(E e) Element is added to the header.
public void addLast(E e) Element is added to the tail.
public boolean offer(E e) Add an element to the end of the list and return success, true for success, false for failure.
public boolean offerFirst(E e) Inserts the element in the header and returns success, true for success, false for failure.
public boolean offerLast(E e) Insert element at the tail, return success, true for success, false for failure.
public void clear() Clear the linked list.
public E removeFirst() Delete and return the first element.
public E removeLast() Removes and returns the last element.
public boolean remove(Object o) Delete an element and return whether it succeeded (true on success, false on failure).
public E remove(int index) Deletes the element at the specified location.
public E poll() Delete and return the first element.
public E remove() Delete and return the first element.
public boolean contains(Object o) Determines whether an element is present.
public E get(int index) Returns the element at the specified position.
public E getFirst() Returns the first element.
public E getLast() Returns the last element.
public int indexOf(Object o) Finds the index of the first occurrence of the specified element from front to back.
public int lastIndexOf(Object o) Finds the index of the last occurrence of the specified element.
public E peek() Returns the first element.
public E element() Returns the first element.
public E peekFirst() Returns the header element.
public E peekLast() Returns the tail element.
public E set(int index, E element) Sets the element at the specified location.
public Object clone() Clone the list.
public Iterator descendingIterator() Returns an inverse-order iterator.
public int size() Returns the number of linked list elements.
public ListIterator listIterator(int index) Returns an iterator from the beginning to the end of the specified position.
public Object[] toArray() Returns an array of linked list elements.