how to create a linked list in java
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 string format with placeholder
- Category
- Java
- Title
- insert element into arraylist java
- Category
- Java
- Title
- java previous permutation
- Category
- Java
- Title
- how to compare current date and time with another date and time in android
- Category
- Java
- Title
- compile java to jar in mac
- Category
- Java
- Title
- java file class
- Category
- Java
- Title
- java loop
- Category
- Java
- Title
- jar file not running after being compiled
- Category
- Java
- Title
- bufferedwriter doesn't write to file
- Category
- Java
- Title
- java set example
- Category
- Java
- Title
- bootstrap alert
- Category
- Java
- Title
- same method name with different arguments
- Category
- Java
- Title
- date to string java
- Category
- Java
- Title
- how take inputs in java with message
- Category
- Java
- Title
- iterator for array java
- Category
- Java
- Title
- java replaceall single character
- Category
- Java
- Title
- parsedouble java
- Category
- Java
- Title
- Java script code for add
- Category
- Java
- Title
- how to know when user is done typing android
- Category
- Java
- Title
- how do you concatenate an int with a string in java
- Category
- Java
- Title
- primitive data types in java
- Category
- Java
- Title
- android java shared preferences remove key
- Category
- Java
- Title
- how to open a text file in java
- Category
- Java
- Title
- islowercase java
- Category
- Java
- Title
- convert every character in string to arraylist jva
- Category
- Java
- Title
- socket in java
- Category
- Java
- Title
- déclarer un tableau en java
- Category
- Java
- Title
- java repository sql find not in list
- Category
- Java
- Title
- demo java file
- Category
- Java
- Title
- java create a set with values
- Category
- Java
- Title
- java next permutation
- Category
- Java
- Title
- iterate through hashmap in java
- Category
- Java
- Title
- java find if element of list in present in another list
- Category
- Java
- Title
- how to read file from assets folder in android
- Category
- Java
- Title
- java android development find element by id
- Category
- Java
- Title
- traversal of list in java
- Category
- Java
- Title
- how to draw a triangle in java
- Category
- Java
- Title
- bracket balancing program in java
- Category
- Java
- Title
- java list all non directory files in the directory
- Category
- Java
- Title
- android dismiss keyboard
- Category
- Java
- Title
- java 8 printstacktrace in system.out
- Category
- Java
- Title
- int to binary string java
- Category
- Java
- Title
- how to open a webpage with java
- Category
- Java
- Title
- python to java converter
- Category
- Java
- Title
- how to add an object to a list of objects in java
- Category
- Java
- Title
- java 8 find min value in array
- Category
- Java
- Title
- javafx textarea size
- Category
- Java
- Title
- print a letter in java
- Category
- Java
- Title
- properties java 8 maven in pom xml
- Category
- Java
- Title
- elasticsearch api code call using highlevelrestclient
- Category
- Java
- Title
- how to reverse a list in java
- Category
- Java
- Title
- how to use an abstract class in java
- Category
- Java
- Title
- java loop object
- Category
- Java
- Title
- rock paper scissors java
- Category
- Java
- Title
- contains example in java
- Category
- Java
- Title
- height constraint layout guideline
- Category
- Java
- Title
- how to check if a list is empty java
- Category
- Java
- Title
- java manacher's algorithm
- Category
- Java
- Title
- java random number between 100 and 999
- Category
- Java
- Title
- convert array to list java
- Category
- Java
- Title
- leap year checker java
- Category
- Java
- Title
- getindexrequest elasticsearch
- Category
- Java
- Title
- input java
- Category
- Java
- Title
- java get an int from a char
- Category
- Java
- Title
- sieve in java
- Category
- Java
- Title
- java scanner input int
- Category
- Java
- Title
- array in line java
- Category
- Java
- Title
- how timer class and display to jtextfield w3schools in java
- Category
- Java
- Title
- how to init an array with objects in java
- Category
- Java
- Title
- java hashmap size
- Category
- Java
- Title
- how to iterate list in java selenium
- Category
- Java
- Title
- how to make a fixed size array in java
- Category
- Java
- Title
- find the triplet sum in java linked list
- Category
- Java
- Title
- android switch on change
- Category
- Java
- Title
- left shift in java
- Category
- Java
- Title
- string to arraylist convert java
- Category
- Java
- Title
- java permutation
- Category
- Java
- Title
- how to change all the text in a textArea java at once
- Category
- Java
- Title
- how to remove spaces from an array in java
- Category
- Java
- Title
- arraylist with values
- Category
- Java
- Title
- install java debian 8
- Category
- Java
- Title
- can we overload a static method in java
- Category
- Java
- Title
- java log base 2
- Category
- Java
- Title
- init admob
- Category
- Java
- Title
- spring code in java
- Category
- Java
- Title
- output statement java
- Category
- Java
- Title
- file with line numbers inserted java
- Category
- Java
- Title
- how to declare array java
- Category
- Java
- Title
- java
- Category
- Java
- Title
- android round double to 2 decimal
- Category
- Java
- Title
- room insert and return id
- Category
- Java
- Title
- java get size of array
- Category
- Java
- Title
- How to create a 2d array in java
- Category
- Java
- Title
- how to create an array in java
- Category
- Java
- Title
- how to count number of words in a string
- Category
- Java
- Title
- java string to double
- Category
- Java
- Title
- java array to arraylist
- Category
- Java
- Title
- java startActivity crash
- Category
- Java
- Title
- how to change the color of a menubar in java
- Category
- Java
- Title
- java list get first element
- Category
- Java