
- Java Tutorial
- Java - Home
- Java - Overview
- Java - Environment Setup
- Java - Basic Syntax
- Java - Object & Classes
- Java - Constructors
- Java - Basic Datatypes
- Java - Variable Types
- Java - Modifier Types
- Java - Basic Operators
- Java - Loop Control
- Java - Decision Making
- Java - Numbers
- Java - Characters
- Java - Strings
- Java - Arrays
- Java - Date & Time
- Java - Regular Expressions
- Java - Methods
- Java - Files and I/O
- Java - Exceptions
- Java - Inner classes
- Java Object Oriented
- Java - Inheritance
- Java - Overriding
- Java - Polymorphism
- Java - Abstraction
- Java - Encapsulation
- Java - Interfaces
- Java - Packages
- Java Advanced
- Java - Data Structures
- Java - Collections
- Java - Generics
- Java - Serialization
- Java - Networking
- Java - Sending Email
- Java - Multithreading
- Java - Applet Basics
- Java - Documentation
- Java Useful Resources
- Java - Questions and Answers
- Java - Quick Guide
- Java - Useful Resources
- Java - Discussion
- Java - Examples
Java Program to Remove elements from the LinkedList
In this article, we will understand how to remove elements from the linked-list.
The java.util.LinkedList class operations perform we can expect for a doubly-linked list. Operations that index into the list will traverse the list from the beginning or the end, whichever is closer to the specified index.
Below is a demonstration of the same −
Suppose our input is −
The list is defined as: [Java, Scala, Python, JavaScript, C++]
The desired output would be −
The list after removing all the elements is: [Python, JavaScript, C++]
Algorithm
Step 1 - START Step 2 - Declare namely Step 3 - Define the values. Step 4 - Display the result Step 5 - Stop
Example 1
Here, we bind all the operations together under the ‘main’ function.
import java.util.LinkedList; public class Demo { public static void main(String args[]){ LinkedList<String> input_list = new LinkedList<String>(); input_list.add("Java"); input_list.add("Scala"); input_list.add("Python"); input_list.add("JavaScript"); input_list.add("C++"); System.out.println("The list is defined as: " + input_list); input_list.remove(); input_list.remove(); System.out.println("The list after removing all the elements is: " + input_list); } }
Output
The list is defined as: [Java, Scala, Python, JavaScript, C++] The list after removing all the elements is: [Python, JavaScript, C++]
Example 2
Here, we encapsulate the operations into functions exhibiting object oriented programming.
import java.util.LinkedList; public class Demo { static void remove_element(LinkedList<String> input_list){ input_list.remove(); input_list.remove(); System.out.println("The list after removing all the elements is: " + input_list); } public static void main(String args[]){ LinkedList<String> input_list = new LinkedList<String>(); input_list.add("Java"); input_list.add("Scala"); input_list.add("Python"); input_list.add("JavaScript"); input_list.add("C++"); System.out.println("The list is defined as: " + input_list); remove_element(input_list); } }
Output
The list is defined as: [Java, Scala, Python, JavaScript, C++] The list after removing all the elements is: [Python, JavaScript, C++]
- Related Articles
- Java Program to Access elements from a LinkedList
- Remove a range of elements from a LinkedList in Java
- Java Program to Add elements to a LinkedList
- Java Program to Remove duplicate elements from ArrayList
- Java program to remove duplicates elements from a List
- Java Program to remove all elements from a set in Java
- Remove a specific element from a LinkedList in Java
- Get first and last elements from Java LinkedList
- How to remove an element from ArrayList or, LinkedList in Java?
- Golang program to remove elements from the linked list
- Java Program to Implement LinkedList
- Create an object array from elements of LinkedList in Java
- Swift Program to Remove All the Elements from the Array
- Golang program to add elements to a linkedlist
- Remove all elements from Java NavigableMap

Advertisements