Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Java LinkedList removeLast() Method

Java LinkedList removeLast() Method

Java LinkedList Java LinkedList


removeLast() is a method in Java's LinkedList is a commonly used method provided by the class for removing and returning the last element in the list. This method belongs to java.util.LinkedList class and is part of the Java Collections Framework.

Method Syntax:



public E removeLast()

Return Value:


Method Characteristics

1. Operation position

removeLast() method specifically operates on the tail of the list, in contrast to removeFirst() method.

2. Time complexity

Because LinkedList : Returns null when the list is emptyremoveLast() method has a time complexity of O(1) because it can directly access the tail node.

3. Exception cases

Calling this method when the list is empty throws NoSuchElementException, so before using it, you usually need to check whether the list is empty.


Usage examples

The following is a complete example showing how to use removeLast() method in the component:

Example

import

java.util.LinkedList

;

public

class

LinkedListDemo

{

   

public

static

void

main

(

String

[

]

args

)

{

       

// Create a LinkedList

        LinkedList

<

String

fruits

=

new

LinkedList

<>

(

)

;

       

       

// Add elements

        fruits.

add

(

"Apple"

)

;

        fruits.

add

(

"Banana"

)

;

        fruits.

add

(

"Cherry"

)

;

        fruits.

add

(

"Date"

)

;

       

       

System

.

out

.

println

(

"Original linked list: "

fruits

)

;

       

       

// Use the removeLast() method

       

String

removedElement

=

fruits.

removeLast

(

)

;

       

       

System

.

out

.

println

(

"Removed element: "

removedElement

)

;

       

System

.

out

.

println

(

"List after operation: "

fruits

)

;

   

}

}

Output result:

原始链表: [Apple, Banana, Cherry, Date]

被移除的元素: Date

操作后的链表: [Apple, Banana, Cherry]


Comparison with Other Methods

1. removeLast() vs removeFirst()

2. removeLast() vs pollLast()

- `removeLast()` throws an exception - `pollLast()` returns null

3. removeLast() vs pop()


Best Practices

1. Empty list check

To avoid NoSuchElementException, it is recommended to check whether the list is empty before using it:

Example

if

(

!

myLinkedList.

isEmpty

(

)

)

{

    myLinkedList.

removeLast

(

)

;

}

2. Alternative

If you do not want to handle exceptions, you can use pollLast() method in the component:

Example

// Returns null if the list is empty

String

lastElement

=

myLinkedList.

pollLast

(

)

;

3. Performance considerations

Because LinkedList 's removeLast() operation is constant time and suitable for frequent tail removal operations. If it is a scenario with frequent random access, consider using ArrayList.


Summary

LinkedList.removeLast() is an efficient method specifically for removing elements from the tail of the list. Understanding this method's behavioral characteristics and its difference from other similar methods can help developers use LinkedList class more effectively. In practical applications, choose the appropriate method based on specific needs and pay attention to handling possible exceptions.

Java LinkedList Java LinkedList