# LinkedList Operations

![](https://2172743581-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-La4k4liN--SZ6ay4iod%2F-LanpOGdgv3_J537-CNX%2F-LanrazgaLyPNUIisHfQ%2Fassets_-LTkdx_CHE-ubFCrKQ7__-L_jhpC3ZhIHyqLEGD0U_-L_jj3jGTY_D48BclKl5_Screen%20Shot%202019-03-12%20at%207.59.46%20AM.png?alt=media\&token=0fb31588-6568-48cc-8b62-ce4125ee28fe)

### 1) poll, pollFirst, pollLast : display plus remove

```java
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);

	}

}
```

```java
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)

```java
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() + "<-- ");
		}

	}

}
```

```java
 Player exited Akash<--  Player exited Shivam<--  Player exited Mohit<--  Player exited Rahul<-- 
```

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

```java
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);

	}

}
```

```java
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

```java
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);

	}

}
```

```java
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 :

```java
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);

	}
}
```

```java
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. [Go to the editor](https://www.w3resource.com/java-exercises/collection/index.php#editorr)\
[Click me to see the solution](https://www.w3resource.com/java-exercises/collection/java-collection-linked-list-exercise-1.php)

**2.** Write a Java program to iterate through all elements in a linked list. [Go to the editor](https://www.w3resource.com/java-exercises/collection/index.php#editorr)\
[Click me to see the solution](https://www.w3resource.com/java-exercises/collection/java-collection-linked-list-exercise-2.php)

**3.** Write a Java program to iterate through all elements in a linked list starting at the specified position. [Go to the editor](https://www.w3resource.com/java-exercises/collection/index.php#editorr)\
[Click me to see the solution](https://www.w3resource.com/java-exercises/collection/java-collection-linked-list-exercise-3.php)

**4.** Write a Java program to iterate a linked list in reverse order. [Go to the editor](https://www.w3resource.com/java-exercises/collection/index.php#editorr)\
[Click me to see the solution](https://www.w3resource.com/java-exercises/collection/java-collection-linked-list-exercise-4.php)

**5.** Write a Java program to insert the specified element at the specified position in the linked list. [Go to the editor](https://www.w3resource.com/java-exercises/collection/index.php#editorr)\
[Click me to see the solution](https://www.w3resource.com/java-exercises/collection/java-collection-linked-list-exercise-5.php)

**6.** Write a Java program to insert elements into the linked list at the first and last position. [Go to the editor](https://www.w3resource.com/java-exercises/collection/index.php#editorr)\
[Click me to see the solution](https://www.w3resource.com/java-exercises/collection/java-collection-linked-list-exercise-6.php)

**7.** Write a Java program to insert the specified element at the front of a linked list. [Go to the editor](https://www.w3resource.com/java-exercises/collection/index.php#editorr)\
[Click me to see the solution](https://www.w3resource.com/java-exercises/collection/java-collection-linked-list-exercise-7.php)

**8.** Write a Java program to insert the specified element at the end of a linked list. [Go to the editor](https://www.w3resource.com/java-exercises/collection/index.php#editorr)\
[Click me to see the solution](https://www.w3resource.com/java-exercises/collection/java-collection-linked-list-exercise-8.php)

**9.** Write a Java program to insert some elements at the specified position into a linked list. [Go to the editor](https://www.w3resource.com/java-exercises/collection/index.php#editorr)\
[Click me to see the solution](https://www.w3resource.com/java-exercises/collection/java-collection-linked-list-exercise-9.php)

**10.** Write a Java program to get the first and last occurrence of the specified elements in a linked list. [Go to the editor](https://www.w3resource.com/java-exercises/collection/index.php#editorr)\
[Click me to see the solution](https://www.w3resource.com/java-exercises/collection/java-collection-linked-list-exercise-10.php)

**11.** Write a Java program to display the elements and their positions in a linked list. [Go to the editor](https://www.w3resource.com/java-exercises/collection/index.php#editorr)\
[Click me to see the solution](https://www.w3resource.com/java-exercises/collection/java-collection-linked-list-exercise-11.php)

**12.** Write a Java program to remove a specified element from a linked list. [Go to the editor](https://www.w3resource.com/java-exercises/collection/index.php#editorr)\
[Click me to see the solution](https://www.w3resource.com/java-exercises/collection/java-collection-linked-list-exercise-12.php)

**13.** Write a Java program to remove first and last element from a linked list. [Go to the editor](https://www.w3resource.com/java-exercises/collection/index.php#editorr)\
[Click me to see the solution](https://www.w3resource.com/java-exercises/collection/java-collection-linked-list-exercise-13.php)

**14.** Write a Java program to remove all the elements from a linked list. [Go to the editor](https://www.w3resource.com/java-exercises/collection/index.php#editorr)\
[Click me to see the solution](https://www.w3resource.com/java-exercises/collection/java-collection-linked-list-exercise-14.php)

**15.** Write a Java program of swap two elements in an linked list. [Go to the editor](https://www.w3resource.com/java-exercises/collection/index.php#editorr)\
[Click me to see the solution](https://www.w3resource.com/java-exercises/collection/java-collection-linked-list-exercise-15.php)

**16.** Write a Java program to shuffle the elements in a linked list. [Go to the editor](https://www.w3resource.com/java-exercises/collection/index.php#editorr)\
[Click me to see the solution](https://www.w3resource.com/java-exercises/collection/java-collection-linked-list-exercise-16.php)

**17.** Write a Java program to join two linked lists. [Go to the editor](https://www.w3resource.com/java-exercises/collection/index.php#editorr)\
[Click me to see the solution](https://www.w3resource.com/java-exercises/collection/java-collection-linked-list-exercise-17.php)

**18.** Write a Java program to clone an linked list to another linked list. [Go to the editor](https://www.w3resource.com/java-exercises/collection/index.php#editorr)\
[Click me to see the solution](https://www.w3resource.com/java-exercises/collection/java-collection-linked-list-exercise-18.php)

**19.** Write a Java program to remove and return the first element of a linked list. [Go to the editor](https://www.w3resource.com/java-exercises/collection/index.php#editorr)\
[Click me to see the solution](https://www.w3resource.com/java-exercises/collection/java-collection-linked-list-exercise-19.php)

**20.** Write a Java program to retrieve but does not remove, the first element of a linked list. [Go to the editor](https://www.w3resource.com/java-exercises/collection/index.php#editorr)\
[Click me to see the solution](https://www.w3resource.com/java-exercises/collection/java-collection-linked-list-exercise-20.php)

**21.** Write a Java program to retrieve but does not remove, the last element of a linked list. [Go to the editor](https://www.w3resource.com/java-exercises/collection/index.php#editorr)\
[Click me to see the solution](https://www.w3resource.com/java-exercises/collection/java-collection-linked-list-exercise-21.php)

**22.** Write a Java program to check if a particular element exists in a linked list. [Go to the editor](https://www.w3resource.com/java-exercises/collection/index.php#editorr)\
[Click me to see the solution](https://www.w3resource.com/java-exercises/collection/java-collection-linked-list-exercise-22.php)

**23.** Write a Java program to convert a linked list to array list. [Go to the editor](https://www.w3resource.com/java-exercises/collection/index.php#editorr)\
[Click me to see the solution](https://www.w3resource.com/java-exercises/collection/java-collection-linked-list-exercise-23.php)

**24.** Write a Java program to compare two linked lists. [Go to the editor](https://www.w3resource.com/java-exercises/collection/index.php#editorr)\
[Click me to see the solution](https://www.w3resource.com/java-exercises/collection/java-collection-linked-list-exercise-24.php)

**25.** Write a Java program to test an linked list is empty or not. [Go to the editor](https://www.w3resource.com/java-exercises/collection/index.php#editorr)\
[Click me to see the solution](https://www.w3resource.com/java-exercises/collection/java-collection-linked-list-exercise-25.php)

**26.** Write a Java program to replace an element in a linked list. [Go to the editor](https://www.w3resource.com/java-exercises/collection/index.php#editorr)\
[Click me to see the solution](https://www.w3resource.com/java-exercises/collection/java-collection-linked-list-exercise-26.php)
