Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

polllast
java
linkedlist
communitycreator

What is the LinkedList.pollLast() method in Java?

Programming Bytes

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Linked list

A linked list is a collection of linear data elements. Each element, or node, contains data and reference parts. The data part contains the value, and the reference part contains the address link to the next element.

The elements are not indexed, so random access is not possible like it would be in Array objects. Instead, we traverse from the beginning of the list and access the elements.

In Java, the LinkedList class is the doubly-linked listKind of linked list. Each node contains three fields: two link fields (one for the previous element and another for the next element) and one data field. implementation of the List and Deque interfaces. The LinkedList class is present in the java.util package.

What is the pollLast() method of the LinkedList class?

The pollLast() method can be used to get and remove the last element of a LinkedList object.

Syntax

The syntax of the pollLast() method is as follows:

public E pollLast();

This method doesn’t take any arguments.

This method retrieves and removes the last element of the specified LinkedList. If the list is empty, null is returned.

Code

The code below demonstrates how to use the pollLast() method:

import java.util.LinkedList;
class LinkedListPollLast {
public static void main( String args[] ) {
LinkedList<String> list = new LinkedList<>();
list.add("1");
list.add("2");
list.add("3");
System.out.println("The list is " + list);
System.out.println("list.pollLast() returns : " + list.pollLast());
System.out.println("The list is " + list);
}
}

Explanation

In the code above:

  • In line number 1, we imported the LinkedList class.
import java.util.LinkedList;
  • In line number 4, we created a LinkedList object with the name list.
LinkedList<String> list = new LinkedList<>();
  • From line number 5 to 7, we used the add() method of the list object to add three elements ("1","2","3") to the list.
list.add("1");
list.add("2");
list.add("3");
  • In line number 10, we used the pollLast() method of the list object to get and remove the last element.
list.pollLast(); // "3"
list; // ["1", "2"]

RELATED TAGS

polllast
java
linkedlist
communitycreator

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Keep Exploring