Linked List implementation
Java
List<Integer> list = new LinkedList<>();public class LinkedList {
private Node head;
private int length = 0;
public LinkedList() {
this.head = new Node(null);
}
public int size() {
return length;
}
// Adds an element to the end of the list
public void add(Object data) {
Node node = new Node(data);
Node iterator = head;
while (iterator.getNext() != null){
iterator = iterator.getNext();
}
iterator.setNext(node);
length++;
}
// Obtains an element by index
public Object get(int index) {
if (head.getNext() == null || index >= length){
return null;
}
Node iterator = head.getNext();
int counter = 0;
while(counter < index){
iterator = iterator.getNext();
counter++;
}
return iterator.getData();
}
// Returns the index of the element in the list
public int indexOf(Object data) {
Node obj=head;
for (int i = 0; i < length; i++) {
obj = obj.getNext();
if (obj.getData().equals(data)) {
return i;
}
}
return -1;
//throw new Exception("Data not found");
}
// Removes an element from the list
public boolean remove(Object data) {
if (head.getNext() == null){
return false;
}
Node iterator = head;
while(iterator.getNext() != null){
if (iterator.getNext().getData().equals(data)){
iterator.setNext(iterator.getNext().getNext());
length--;
return true;
}
iterator = iterator.getNext();
}
return false;
}
private class Node {
private Object data;
private Node next;
public Node(Object data) {
this.data = data;
next = null;
}
public Object getData() {
return data;
}
public void setData(Object data) {
this.data = data;
}
public Node getNext() {
return next;
}
public void setNext(Node next) {
this.next = next;
}
}
}
Also in Java:
- Title
- java stream sort Collator
- Category
- Java
- Title
- how to do a linear searc in java
- Category
- Java
- Title
- java doreturn void
- Category
- Java
- Title
- How to find the length of an array in java
- Category
- Java
- Title
- arrays.aslist.add
- Category
- Java
- Title
- abstract class in java
- Category
- Java
- Title
- java string split from input string
- Category
- Java
- Title
- mettre caractère de string en majuscule java
- Category
- Java
- Title
- java array
- Category
- Java
- Title
- google map set default zoom android studio
- Category
- Java
- Title
- validation list empty java
- Category
- Java
- Title
- array declaration and initialization in java
- Category
- Java
- Title
- string startswith java
- Category
- Java
- Title
- android studio clear views of layout
- Category
- Java
- Title
- Compilation is not supported for following modules: android. Unfortunately you can't have non-Gradle Java modules and Android-Gradle modules in one project
- Category
- Java
- Title
- java type casting
- Category
- Java
- Title
- how to see if a shape is touching another shape in java
- Category
- Java
- Title
- icon label java
- Category
- Java
- Title
- declareing a variable in java
- Category
- Java
- Title
- java create jframe
- Category
- Java
- Title
- enumerated data types in java
- Category
- Java
- Title
- java int stream min
- Category
- Java
- Title
- java change boolean to opposite
- Category
- Java
- Title
- java create window
- Category
- Java
- Title
- how to set frame colo in java
- Category
- Java
- Title
- how to read a csv file java
- Category
- Java
- Title
- java.lang.long cannot be cast to java.lang.integer
- Category
- Java
- Title
- spring org.springframework.stereotype not visible
- Category
- Java
- Title
- java int array
- Category
- Java
- Title
- linux change java
- Category
- Java
- Title
- java arraylist add to top
- Category
- Java
- Title
- boolean java.lang.String.equals(java.lang.Object)' on a null object reference
- Category
- Java
- Title
- Fragment DashboardFragment not associated with a fragment manager.
- Category
- Java
- Title
- for each loop java
- Category
- Java
- Title
- get current unix timestamp java
- Category
- Java
- Title
- how-to-use-volley-string-request-in-android
- Category
- Java
- Title
- list java oracle
- Category
- Java
- Title
- arraylist get last
- Category
- Java
- Title
- Error executing Maven. java.io.FileNotFoundException: The specified user settings file does not exist: /etc/java-8-openjdk
- Category
- Java
- Title
- double round java integer
- Category
- Java
- Title
- what it means when create final variable in java
- Category
- Java
- Title
- prime number program in java
- Category
- Java
- Title
- how to add all list elements at once in java
- Category
- Java
- Title
- java put a char array into a string
- Category
- Java
- Title
- dates in java 8
- Category
- Java
- Title
- bukkit java get player count
- Category
- Java
- Title
- java concatenate strings
- Category
- Java
- Title
- how to pass enum in postman body
- Category
- Java
- Title
- using java 8 stream to process data in java
- Category
- Java
- Title
- decomposer chaine de caractère java
- Category
- Java
- Title
- how to detect device javascirpt
- Category
- Java
- Title
- copy file with byte java
- Category
- Java
- Title
- pascals triangle java
- Category
- Java
- Title
- add video in bootstrap
- Category
- Java
- Title
- java strin to int
- Category
- Java
- Title
- java console write
- Category
- Java
- Title
- creating thread in java example
- Category
- Java
- Title
- when to use serializable in java
- Category
- Java
- Title
- imageview.setbackground
- Category
- Java
- Title
- read a file in java and store as integer array using buffered reader
- Category
- Java
- Title
- big integer java
- Category
- Java
- Title
- java recursion
- Category
- Java
- Title
- non primitive data types in java
- Category
- Java
- Title
- char array to string java
- Category
- Java
- Title
- string to char in java
- Category
- Java
- Title
- java get folder content
- Category
- Java
- Title
- java create txt file
- Category
- Java
- Title
- java text ouput to console
- Category
- Java
- Title
- online money transfer andhra bank
- Category
- Java
- Title
- android get last crash adb
- Category
- Java
- Title
- java convert char to int
- Category
- Java
- Title
- java.sql.SQLException: Before start of result set
- Category
- Java
- Title
- java convert string to int array
- Category
- Java
- Title
- string remove last character
- Category
- Java
- Title
- integer to string java
- Category
- Java
- Title
- java long to int
- Category
- Java
- Title
- Character.getNumericValue['z']
- Category
- Java
- Title
- import java.util.hashset
- Category
- Java
- Title
- how to skip a line in java
- Category
- Java
- Title
- how to close a jframe in java with an if statement
- Category
- Java
- Title
- java djikstra's algorithm
- Category
- Java
- Title
- javafx textarea how to make smaller
- Category
- Java
- Title
- java length of matrix
- Category
- Java
- Title
- hello world in java
- Category
- Java
- Title
- java declare an int inside an if statement
- Category
- Java
- Title
- how to copy all files and subdirectories in directory in java
- Category
- Java
- Title
- java string format with placeholder
- Category
- Java
- Title
- java variable declaration
- Category
- Java
- Title
- Unhandled exception: java.lang.InterruptedException
- Category
- Java
- Title
- how to write a java for loop?
- Category
- Java
- Title
- place.getlatlng() returning null
- Category
- Java
- Title
- Java Read Files
- Category
- Java
- Title
- Failed to apply plugin [class 'com.google.gms.googleservices.GoogleServicesPlugin']
- Category
- Java
- Title
- sublist in java
- Category
- Java
- Title
- how to reverse a list in java
- Category
- Java
- Title
- java substring
- Category
- Java
- Title
- compile java
- Category
- Java
- Title
- how to read to into a file in java
- Category
- Java
- Title
- Java array equals
- Category
- Java
- Title
- java get first char
- Category
- Java