Core java - Advance Topics
  • Welcome
  • Schedule
  • 1) Exception Handling
    • 1) Introduction to Exception Handling
    • 2) Categories of Exceptions
    • 3) Creating a method that throws an exception
    • 4) Creating Custom Exception Classes
    • 5)What happens when an exception is thrown?
      • 5.1) Creating try-catch-finally blocks
      • 5.2) Using a method that throws a checked exception
      • 5.3) Using a method that throws a runtime exception
      • 5.4) Using a method that throws an error
      • 5.5) Will a finally block execute even if the catch block defines a return statement?
      • 5.6) What happens if both a catch and a finally block define return statement?
      • 5.7) What happens if a finally block modifies the value returned from a catch block?
      • 5.8) Can a try block be followed only by a finally block?
      • 5.9) Does the order of the exceptions caught in the catch blocks matter?
      • 5.10) Can I rethrow an exception or the error I catch?
      • 5.11) Can I declare my methods to throw a checked exception instead of handling it?
      • 5.12) I can create nested loops, so can I create nested try-catch blocks too?
      • 5.13) Should I handle errors?
    • 6) Best Practices
    • 7) Cheat Sheet
    • 8) Problems
  • 2) Wrapper Classes and Enums
    • 2.1) Creating objects of the wrapper classes
    • Enums
  • 3) Inner Classes
    • 3.1) Static nested class (also called static inner class)
    • 3.2) Inner class (also called member class)
    • 3.3) Anonymous inner class
    • 3.4) Method local inner classes
    • CheatSheet
  • 4) Generics
    • Multiple Type parameters in Generic classes
    • Inheritance using Generics
    • Generic interfaces
    • Generic Methods
    • Bounded type parameters
    • Applications
  • 5) Equals and Hashcode
    • Problems
  • CompareTo method overview
  • Basic DS
    • 1) Simple Array List
    • 2) Simple HashMap
  • 5) Collections Framework - Part 1
    • Introducing the collections framework
    • Working with the Collection interface
      • The core Collection interface
      • Methods of the Collection interface
    • Creating and using List, Set, and Deque implementations
      • List interface and its implementations
      • Iterators
      • Sorting List using custom sorting technique
      • Comparable Interface
      • Custom Sorting using comparator
      • ArrayList - Examples and practice problems
    • Stack
    • Linked List
    • LinkedList Operations
  • 6) Collections Framework - Part 2
    • Sets
      • Set Types
      • Array to Set (vice versa)
    • Maps
    • TreeMap
    • Autoboxing And Unboxing
  • Collections Framework - Part 3
    • Basics : DS , Number System
    • Internal Working
      • HashMap
      • HashSet
  • 7) Reflection API
  • 8) Annotations
  • 9) Reading Input From Various Sources
    • File Handling
    • Reading From Xml
    • Reading From JSON
  • 10) Multi-threading (Concurrency)
    • Protect shared data
    • Thread-safe access to shared data
  • 11) Design Patterns
    • Singleton
    • DI
  • 12) Internal Working of JVM
  • 13) Garbage Collection
  • 14) More on Strings (Buffer and Builder)
  • 15) Cloning and Immutable Class
    • 16) Serialization And Deserialization
    • Untitled
  • JAVA 8
    • Interface Changes
    • Lambda
    • Method Ref
    • Optional
    • Streams
    • Predicates
  • Practice Tests
    • Test - Collections
    • OOPS
    • S-OOPS
Powered by GitBook
On this page
  • 1) poll, pollFirst, pollLast : display plus remove
  • Example 2: (Queue using LinkedList)
  • 2) peek, peekFirst , peekLast : display don't remove:
  • 3) offer, offerFirst, offerLast : to add element at first or last
  • 4) indexOf , lastIndexOf :
  • Problems:

Was this helpful?

  1. 5) Collections Framework - Part 1

LinkedList Operations

PreviousLinked ListNext6) Collections Framework - Part 2

Last updated 6 years ago

Was this helpful?

1) poll, pollFirst, pollLast : display plus remove

import java.util.LinkedList;

public class LinkedListExample {
	public static void main(String[] args) {

		LinkedList<Integer> linkedList = new LinkedList<Integer>();

		linkedList.add(23);
		linkedList.add(24);
		linkedList.add(25);
		linkedList.add(28);
		linkedList.add(29);

		System.out.println("Linked list before poll() ");
		System.out.println(linkedList);
		linkedList.poll();
		System.out.println("Linked list after poll() ");
		System.out.println(linkedList);

		System.out.println("Linked list before pollFirst() ");
		System.out.println(linkedList);
		linkedList.pollFirst();
		System.out.println("Linked list after pollFirst() ");
		System.out.println(linkedList);

		System.out.println("Linked list before pollLast() ");
		System.out.println(linkedList);
		linkedList.pollLast();
		System.out.println("Linked list after pollLast() ");
		System.out.println(linkedList);

	}

}
Linked list before poll() 
[23, 24, 25, 28, 29]
Linked list after poll() 
[24, 25, 28, 29]
Linked list before pollFirst() 
[24, 25, 28, 29]
Linked list after pollFirst() 
[25, 28, 29]
Linked list before pollLast() 
[25, 28, 29]
Linked list after pollLast() 
[25, 28]

Example 2: (Queue using LinkedList)

import java.util.LinkedList;

public class PlayersLLExample {
	public static void main(String[] args) {

		LinkedList<String> players = new LinkedList<String>();

		players.add("Akash");
		players.add("Shivam");
		players.add("Mohit");
		players.add("Rahul");

		while (!players.isEmpty()) {
			System.out.print(" Player exited " + players.poll() + "<-- ");
		}

	}

}
 Player exited Akash<--  Player exited Shivam<--  Player exited Mohit<--  Player exited Rahul<-- 

2) peek, peekFirst , peekLast : display don't remove:

import java.util.LinkedList;

public class LinkedListExample {
	public static void main(String[] args) {

		LinkedList<Integer> linkedList = new LinkedList<Integer>();

		linkedList.add(23);
		linkedList.add(24);
		linkedList.add(25);
		linkedList.add(28);
		linkedList.add(29);

		System.out.println("Linked list before peek() ");
		System.out.println(linkedList);
		System.out.println("element peeked " + linkedList.peek());
		System.out.println("Linked list after peek() ");
		System.out.println(linkedList);

		System.out.println("Linked list before peekFirst() ");
		System.out.println(linkedList);
		System.out.println("element peeked " + linkedList.peekFirst());
		System.out.println("Linked list after peekFirst() ");
		System.out.println(linkedList);

		System.out.println("Linked list before peekLast() ");
		System.out.println(linkedList);
		System.out.println("element peeked " + linkedList.peekLast());
		System.out.println("Linked list after peekLast() ");
		System.out.println(linkedList);

	}

}
Linked list before peek() 
[23, 24, 25, 28, 29]
element peeked 23
Linked list after peek() 
[23, 24, 25, 28, 29]
Linked list before peekFirst() 
[23, 24, 25, 28, 29]
element peeked 23
Linked list after peekFirst() 
[23, 24, 25, 28, 29]
Linked list before peekLast() 
[23, 24, 25, 28, 29]
element peeked 29
Linked list after peekLast() 
[23, 24, 25, 28, 29]

3) offer, offerFirst, offerLast : to add element at first or last

import java.util.LinkedList;

public class LinkedListExampleOp2 {
	public static void main(String[] args) {

		LinkedList<Integer> linkedList = new LinkedList<Integer>();

		linkedList.add(23);
		linkedList.add(24);
		linkedList.add(25);
		linkedList.add(28);
		linkedList.add(29);

		System.out.println("Linked list before offer(someInt) ");
		System.out.println(linkedList);
		System.out.println("element offered " + linkedList.offer(30));
		System.out.println("Linked list after offer(someInt) ");
		System.out.println(linkedList);

		System.out.println("Linked list before offerFirst(someInt) ");
		System.out.println(linkedList);
		System.out.println("element offered " + linkedList.offerFirst(31));
		System.out.println("Linked list after offerFirst(someInt) ");
		System.out.println(linkedList);

		System.out.println("Linked list before offerLast(someInt) ");
		System.out.println(linkedList);
		System.out.println("element offered " + linkedList.offerLast(32));
		System.out.println("Linked list after offerLast(someInt) ");
		System.out.println(linkedList);

	}

}
Linked list before offer(someInt) 
[23, 24, 25, 28, 29]
element offered true
Linked list after offer(someInt) 
[23, 24, 25, 28, 29, 30]
Linked list before offerFirst(someInt) 
[23, 24, 25, 28, 29, 30]
element offered true
Linked list after offerFirst(someInt) 
[31, 23, 24, 25, 28, 29, 30]
Linked list before offerLast(someInt) 
[31, 23, 24, 25, 28, 29, 30]
element offered true
Linked list after offerLast(someInt) 
[31, 23, 24, 25, 28, 29, 30, 32]

4) indexOf , lastIndexOf :

import java.util.LinkedList;

public class LinkedListIndexOfEx {
	public static void main(String[] args) {
		LinkedList<Integer> linkedList = new LinkedList<Integer>();

		linkedList.add(23); // 0
		linkedList.add(24); // 1
		linkedList.add(25); // 2 -- first index of 25
		linkedList.add(28); // 3
		linkedList.add(25); // 4
		linkedList.add(29); // 5
		linkedList.add(25); // 6 -- last index of 25
		linkedList.add(31); // 7

		int index = linkedList.indexOf(25);

		System.out.println("first occurence index of 25 is " + index);

		index = linkedList.lastIndexOf(25);

		System.out.println("last occurence index of 25 is " + index);

	}
}
first occurence index of 25 is 2
last occurence index of 25 is 6

Problems:

1. Write a Java program to append the specified element to the end of a linked list.

2. Write a Java program to iterate through all elements in a linked list.

3. Write a Java program to iterate through all elements in a linked list starting at the specified position.

4. Write a Java program to iterate a linked list in reverse order.

5. Write a Java program to insert the specified element at the specified position in the linked list.

6. Write a Java program to insert elements into the linked list at the first and last position.

7. Write a Java program to insert the specified element at the front of a linked list.

8. Write a Java program to insert the specified element at the end of a linked list.

9. Write a Java program to insert some elements at the specified position into a linked list.

10. Write a Java program to get the first and last occurrence of the specified elements in a linked list.

11. Write a Java program to display the elements and their positions in a linked list.

12. Write a Java program to remove a specified element from a linked list.

13. Write a Java program to remove first and last element from a linked list.

14. Write a Java program to remove all the elements from a linked list.

15. Write a Java program of swap two elements in an linked list.

16. Write a Java program to shuffle the elements in a linked list.

17. Write a Java program to join two linked lists.

18. Write a Java program to clone an linked list to another linked list.

19. Write a Java program to remove and return the first element of a linked list.

20. Write a Java program to retrieve but does not remove, the first element of a linked list.

21. Write a Java program to retrieve but does not remove, the last element of a linked list.

22. Write a Java program to check if a particular element exists in a linked list.

23. Write a Java program to convert a linked list to array list.

24. Write a Java program to compare two linked lists.

25. Write a Java program to test an linked list is empty or not.

26. Write a Java program to replace an element in a linked list.

Go to the editor
Click me to see the solution
Go to the editor
Click me to see the solution
Go to the editor
Click me to see the solution
Go to the editor
Click me to see the solution
Go to the editor
Click me to see the solution
Go to the editor
Click me to see the solution
Go to the editor
Click me to see the solution
Go to the editor
Click me to see the solution
Go to the editor
Click me to see the solution
Go to the editor
Click me to see the solution
Go to the editor
Click me to see the solution
Go to the editor
Click me to see the solution
Go to the editor
Click me to see the solution
Go to the editor
Click me to see the solution
Go to the editor
Click me to see the solution
Go to the editor
Click me to see the solution
Go to the editor
Click me to see the solution
Go to the editor
Click me to see the solution
Go to the editor
Click me to see the solution
Go to the editor
Click me to see the solution
Go to the editor
Click me to see the solution
Go to the editor
Click me to see the solution
Go to the editor
Click me to see the solution
Go to the editor
Click me to see the solution
Go to the editor
Click me to see the solution
Go to the editor
Click me to see the solution