LinkedList remove() method with example in java

Published by user on

1. remove()
The remove() method of LinkedList retrieves and removes the first element in this list. It throws a NoSuchElementException exception if this list is empty.

Syntax

Object remove()

Parameters

It doesn't take any parameter

Return Value

the removed element in this list

Exception

 It throws NoSuchElementException exception if this list is empty.

2. remove(int index)
The remove(int index) of LinkedList retrieves and removes the element at the specified index in this linked-list. It throws IndexOutOfBoundsException exception if the index is out of range. (index < 0 || index >= size())

Syntax

Object remove()

Parameters

It doesn't take any parameter

Return Value

the removed element in this list

Exception

 It throws IndexOutOfBoundsException  exception if this list is empty.

3. remove(E element)
The remove(E element) of LinkedList removes the first occurrence of the specified element from this linked list if it is present. If the list doesn’t contain the specified element then it remains unchanged.

Syntax

boolean remove(E e)

Parameters

e is the element to remove from this linked-list

Return Value

true on successful removal of the element else false if the list does not contain the specified element.

Program 1

import java.util.LinkedList;

public class LinkedListRemoveExample {

	public static void main(String[] args) {

		LinkedList<Integer> oddNumbers = new LinkedList<>();
		oddNumbers.add(5);
		oddNumbers.add(7);
		oddNumbers.add(9);
		oddNumbers.add(11);

		System.out.println("Linked List elements: " + oddNumbers);
		oddNumbers.remove();
		System.out.println("Linked List after removing first element: " + oddNumbers);

	}
}

Output

Linked List elements: [5, 7, 9, 11]
Linked List after removing first element: [7, 9, 11]

Program 2

import java.util.LinkedList;

public class LinkedListRemoveExample {

	public static void main(String[] args) {

		LinkedList<Integer> oddNumbers = new LinkedList<>();
		oddNumbers.add(5);
		oddNumbers.add(7);
		oddNumbers.add(9);
		oddNumbers.add(11);

		System.out.println("Linked List elements: " + oddNumbers);

		oddNumbers.clear();
		System.out.println("Linked List elements after clear() call: " + oddNumbers);
		oddNumbers.remove();//throws NoSuchElementException as linked-list is empty
	}
}

Output

Linked List elements: [5, 7, 9, 11]
Linked List elements after clear() call: []
Exception in thread "main" java.util.NoSuchElementException
	at java.util.LinkedList.removeFirst(LinkedList.java:270)
	at java.util.LinkedList.remove(LinkedList.java:685)
	at LinkedListRemoveExample.main(LinkedListRemoveExample.java:19)

Program 3

import java.util.LinkedList;

public class LinkedListRemoveExample {

	public static void main(String[] args) {

		LinkedList<Integer> oddNumbers = new LinkedList<>();
		oddNumbers.add(5);
		oddNumbers.add(7);
		oddNumbers.add(9);
		oddNumbers.add(11);

		System.out.println("Linked List elements: " + oddNumbers);

		oddNumbers.remove(2);
		System.out.println("Linked List after removing element at index 2: " + oddNumbers);
	}
}

Output

Linked List elements: [5, 7, 9, 11]
Linked List after removing element at index 2: [5, 7, 11]

Program 4

import java.util.LinkedList;

public class LinkedListRemoveExample {

	public static void main(String[] args) {

		LinkedList<Integer> oddNumbers = new LinkedList<>();
		oddNumbers.add(5);
		oddNumbers.add(7);
		oddNumbers.add(9);
		oddNumbers.add(11);

		System.out.println("Linked List elements: " + oddNumbers);

		oddNumbers.remove(7);// throws IndexOutOfBoundsException
	}
}

Output

Linked List elements: [5, 7, 9, 11]
Exception in thread "main" java.lang.IndexOutOfBoundsException: Index: 7, Size: 4
	at java.util.LinkedList.checkElementIndex(LinkedList.java:555)
	at java.util.LinkedList.remove(LinkedList.java:525)
	at LinkedListRemoveExample.main(LinkedListRemoveExample.java:17)

Program 5

import java.util.LinkedList;

public class LinkedListRemoveExample {

	public static void main(String[] args) {

		LinkedList<Integer> oddNumbers = new LinkedList<>();
		oddNumbers.add(5);
		oddNumbers.add(7);
		oddNumbers.add(9);
		oddNumbers.add(11);

		System.out.println("Linked List elements: " + oddNumbers);

		Integer i = new Integer(9);
		oddNumbers.remove(i);
		System.out.println("Linked List after removing element 9: " + oddNumbers);
	}
}

Output

Linked List elements: [5, 7, 9, 11]
Linked List after removing element 9: [5, 7, 11]
Categories: Java

0 Comments

Leave a Reply

Avatar placeholder

Your email address will not be published. Required fields are marked *