LinkedList is a crucial data structure in Computer Science. It is often used in programming interviews to assess a candidate’s grip on fundamentals. Many problems involve using linked lists and dealing with corner cases, so every candidate preparing for interviews must be familiar with this topic. This article discusses all the essential details regarding linked lists that you must know before your software engineering interview.
LinkedList Class Implementation in Java
Let us now look at how LinkedList Class is implemented in Java with the help of an example.
Code:
import java.util.LinkedList;
public class Main {
public static void main(String[] args)Â {
// creating a LinkedList in Java.
LinkedList<String> linkedList = new LinkedList<>();
// Adding elements into the LinkedList
linkedList.add(“Prepare”);
linkedList.add(“for”);
linkedList.add(“interviews”);
linkedList.add(“from”);
linkedList.add(“InterviewKickstart”);
// Printing all the elements in the linkedList.
System.out.println(“Initial Elements: “+linkedList);
// Insert an element into the linkedlist at a specified index
// Here we insert an element at position 2
linkedList.add(2, “software”);
// print the updated linkedlist
System.out.println(“Updated Elements after insert at index 2: “+linkedList);
// Insert at the beginning of list
linkedList.addFirst(“Hey!”);
// print the updated linkedlist
System.out.println(“Updated Elements after inserting at the beginning: “+linkedList);
// Insert at the end of list
linkedList.addLast(“All the Best!”);
// print the updated linkedlist
System.out.println(“Updated Elements after inserting at the end: “+linkedList);
// remove an element from the list if present.
linkedList.remove(“software”);
// print the updated linkedlist
System.out.println(“Updated Elements after removing the string – software: “+linkedList);
// remove an element from the list if present.
linkedList.remove(“notPresent”);
// print the updated linkedlist
System.out.println(“Updated Elements after removing the string – notPresent: “+linkedList);
// remove an element from the specified index.
linkedList.remove(0);
// print the updated linkedlist
System.out.println(“Updated Elements after removing the index 0: “+linkedList);
//set the value at a specified index.
linkedList.set(2 , “software interviews”);
// print the updated linkedlist
System.out.println(“Updated Elements setting the value at index 2: “+linkedList);
// check if the list contains the value ‘InterviewKickstart’.
System.out.println(“Does the list contain the value ‘InterviewKickstart’ “+linkedList.contains(“InterviewKickstart”));
// check if the list contains the value ‘code’.
System.out.println(“Does the list contain the value ‘code’ “+linkedList.contains(“code”));
LinkedList<String> dummyLinkedList = new LinkedList<>();
dummyLinkedList.add(“I am ready”);
dummyLinkedList.add(“for my next”);
dummyLinkedList.add(“software”);
dummyLinkedList.add(“interview”);
// add all the elements from the dummyLinkedList to our original LinkedList.
linkedList.addAll(dummyLinkedList);
System.out.println(“Elements after adding all elements from dummy list: “+linkedList);
// get the element at an index from the linkedList.
// elements in linkedList are 0-indexed.
System.out.println(“Element at the index 4 in linkedList is: ” + linkedList.get(4));
// indexOf returns the index of the first occurrence of an element if present in linkedList.
// if not present it returns -1;
System.out.println(“Index of ‘InterviewKickstart’ is “+linkedList.indexOf(“InterviewKickstart”));
System.out.println(“Index of ‘code’ is “+linkedList.indexOf(“code”));
// poll() Retrieves and removes the first element (head) of the list.
// so it returns the head of this list, or null if this list is empty.
System.out.println(“List before polling: “+linkedList);
linkedList.poll();
System.out.println(“List after polling: “+linkedList);
}
}
Output:
Initial Elements: [Prepare, for, interviews, from, InterviewKickstart]
Updated Elements after insert at index 2: [Prepare, for, software, interviews, from, InterviewKickstart]
Updated Elements after inserting at the beginning: [Hey!, Prepare, for, software, interviews, from, InterviewKickstart]
Updated Elements after inserting at the end: [Hey!, Prepare, for, software, interviews, from, InterviewKickstart, All the Best!]
Updated Elements after removing the string – software: [Hey!, Prepare, for, interviews, from, InterviewKickstart, All the Best!]
Updated Elements after removing the string – notPresent: [Hey!, Prepare, for, interviews, from, InterviewKickstart, All the Best!]
Updated Elements after removing the index 0: [Prepare, for, interviews, from, InterviewKickstart, All the Best!]
Updated Elements setting the value at index 2: [Prepare, for, software interviews, from, InterviewKickstart, All the Best!]
Does the list contain the value ‘InterviewKickstart’ true
Does the list contain the value ‘code’ false
Elements after adding all elements from dummy list: [Prepare, for, software interviews, from, InterviewKickstart, All the Best!, I am ready, for my next, software, interview]
Element at the index 4 in linkedList is: InterviewKickstart
Index of ‘InterviewKickstart’ is 4
Index of ‘code’ is -1
List before polling: [Prepare, for, software interviews, from, InterviewKickstart, All the Best!, I am ready, for my next, software, interview]
List after polling: [for, software interviews, from, InterviewKickstart, All the Best!, I am ready, for my next, software, interview]
Explanation:
- Firstly, we start by creating a linked list and then insert elements into it. So the list contains :Â Â [Prepare, for, software, interviews, from, InterviewKickstart]
- Next, we insert “software†at index 2 . So the updated linkedList will be : [Prepare, for, software, interviews, from, InterviewKickstart]
- Next, we insert “Hey!“ at the beginning of the linkedList : [Hey!, Prepare, for, software, interviews, from, InterviewKickstart]
- Next, we insert “All the Best“ at the end of the linkedList : [Hey!, Prepare, for, software, interviews, from, InterviewKickstart, All the Best!]
- Now we try to remove elements from the list. We first remove “softwareâ€. Result – [Hey!, Prepare, for, interviews, from, InterviewKickstart, All the Best!]
- We then first remove “notPresentâ€. Result remains the same as the string is not present in the linked list.
- Next, we remove the string at index 0, and we get the resultant linkedList as [Prepare, for, interviews, from, InterviewKickstart, All the Best!]
- Finally, we change the value at position 2 to “software interviews†using the set method of the linked list class.Â
Result:
[Prepare, for, software interviews, from, InterviewKickstart, All the Best!]
Next, we check if the list contains the word ‘InterviewKickstart’, and the result we get is true. Further, we check if the list includes the word ‘code’, and the result we get is false.
Next, we create a dummy list and add all elements of it into our original list using the addAll method. So the output is :
[Prepare, for, software interviews, from, InterviewKickstart, All the Best!, I am ready, for my next, software, interview]
Next, we explore the indexOf method, which returns the index of the first occurrence of the element in the list if it is present; else, it returns -1. So for the indexOf(‘InterviewKickstart’), we get output as 4 as its location is at index 4.
For indexOf(‘code’), we get output as -1, as code is not present in our list.
Finally, we explore the poll() method that retrieves and removes the list’s head (first element). Hence we get output as:
 [for, software interviews, from, InterviewKickstart, All the Best!, I am ready, for my next, software, interview]