LinkedList pollLast() trả về phần tử cuối cùng và xoá nó ra khởi LinkedList.
Syntax
public E pollLast();
Ví dụ
import java.util.LinkedList; public class Main { public static void main(String args[]) { LinkedList<String> linkedList = new LinkedList<>(); linkedList.add("HCMUS"); linkedList.add("UIT"); linkedList.add("HCMUT"); System.out.println("LinkedList ban dau: " + linkedList); System.out.println("Phan tu cuoi cung: " + linkedList.pollLast()); System.out.println("LinkedList sau pollLast: " + linkedList); } }
Output:
LinkedList ban dau: [HCMUS, UIT, HCMUT]
Phan tu cuoi cung: HCMUT
LinkedList sau pollLast: [HCMUS, UIT]
Note: Nếu LinkedList rỗng thì pollLast() sẽ trả về null