Home  >  Article  >  Java  >  Use the removeFirst() method of the LinkedList class to delete the first element in the linked list

Use the removeFirst() method of the LinkedList class to delete the first element in the linked list

WBOY
WBOYOriginal
2023-07-25 10:49:551422browse

Use the removeFirst() method of the LinkedList class to delete the first element in the linked list

LinkedList is a commonly used linked list data structure in Java. It implements the List interface and the Deque interface and can be used to store and operate on a series of elements. In the LinkedList class, we can use the removeFirst() method to delete the first element in the linked list.

The sample code is as follows:

import java.util.LinkedList;

public class RemoveFirstExample {
    public static void main(String[] args) {
        // 创建一个LinkedList对象
        LinkedList<String> linkedList = new LinkedList<>();
        
        // 向链表中添加元素
        linkedList.add("A");
        linkedList.add("B");
        linkedList.add("C");
        
        System.out.println("删除前的链表:" + linkedList);
        
        // 使用removeFirst()方法删除链表中的第一个元素
        String removedElement = linkedList.removeFirst();
        
        System.out.println("被删除的元素:" + removedElement);
        System.out.println("删除后的链表:" + linkedList);
    }
}

In the above code, we first created a LinkedListf7e83be87db5cd2d9a8a0b8117b38cd4 object, and then added three elements to the linked list" A", "B", "C". Next, we use the removeFirst() method to delete the first element in the linked list and store the deleted element in a variable removedElement. Finally, we print out the deleted elements and the deleted linked list respectively.

Run the above code, we will get the following output:

删除前的链表:[A, B, C]
被删除的元素:A
删除后的链表:[B, C]

As can be seen from the output, the original linked list contains elements "A", "B", and "C" . After the removeFirst() method is called, the deleted element is "A", and only elements "B" and "C" remain in the deleted linked list.

In addition to the removeFirst() method, the LinkedList class also provides the remove() method to delete the element at the specified position, and the removeLast() method to delete the last element in the linked list. Depending on the needs, we can flexibly use these methods to delete elements in the linked list.

In short, the removeFirst() method of the LinkedList class is a convenient and fast way to delete the first element in the linked list. By saving the removed element in a variable we can process it further. In actual development, we can choose the appropriate method to delete elements according to specific needs, and flexibly use the LinkedList class to operate linked list data.

The above is the detailed content of Use the removeFirst() method of the LinkedList class to delete the first element in the linked list. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn