Java LinkedList
A linked list is a common fundamental data structure. It is a linear list, but it does not store data in linear order; instead, each node stores the address of the next node.
Linked lists can be divided into singly linked lists and doubly linked lists.
A singly linked list contains two values: the value of the current node and a link to the next node.

A doubly linked list contains three values: the value, the backward node link, and the forward node link.

Java LinkedList is similar to ArrayList and is a commonly used data container.
Compared with ArrayList, LinkedList has higher efficiency for insertion and deletion operations, while lower efficiency for search and modification operations.
Use ArrayList in the following cases:
- Frequently accessing a certain element in the list.
- Only need to add and delete elements at the end of the list.
Use LinkedList in the following cases:
- You need to access certain elements in the list through loop iteration.
- You need to frequently add and delete elements at the beginning, middle, end, and other positions of the list.
LinkedList extends the AbstractSequentialList class.
LinkedList implements the Queue interface and can be used as a queue.
LinkedList implements the List interface and can perform list-related operations.
LinkedList implements the Deque interface and can be used as a queue.
LinkedList implements the Cloneable interface and can be cloned.
LinkedList implements the java.io.Serializable interface, which means it supports serialization and can be transmitted through serialization.

The LinkedList class is located in the java.util package and needs to be imported before use. The syntax is as follows:
// Import LinkedList class
import java.util.LinkedList;
LinkedList list = new LinkedList(); // 普通创建方法
或者
LinkedList list = new LinkedList(Collection extends E> c); // 使用集合创建链表
Create a simple linked list instance:
Example
// Import the LinkedList class
import
java.util.LinkedList
;
public
class
RunoobTest
{
public
static
void
main
(
String
[
]
args
)
{
LinkedList
<
String
sites
=
new
LinkedList
<
String
(
);
sites.
add
(
"Google"
)
;
sites.
add
(
"Runoob"
)
;
sites.
add
(
"Taobao"
)
;
sites.
add
(
"Weibo"
)
;
System
.
out
.
println
(
sites
)
;
}
}
The ArrayList class provides many useful methods; to add elements to an ArrayList you can use the add() method:
[Google, Runoob, Taobao, Weibo]
In more cases, using ArrayList to access random elements in a list is more efficient, but LinkedList provides more efficient methods in the following situations.
Add an element at the beginning of the list:
Example
// Import the LinkedList class
import
java.util.LinkedList
;
public
class
RunoobTest
{
public
static
void
main
(
String
[
]
args
)
{
LinkedList
<
String
sites
=
new
LinkedList
<
String
(
);
sites.
add
(
"Google"
)
;
sites.
add
(
"Runoob"
)
;
sites.
add
(
"Taobao"
)
;
// Use addFirst() to add an element at the head
sites.
addFirst
(
"Wiki"
)
;
System
.
out
.
println
(
sites
)
;
}
}
The ArrayList class provides many useful methods; to add elements to an ArrayList you can use the add() method:
[Wiki, Google, Runoob, Taobao]
Add an element at the end of the list:
Example
// Import the LinkedList class
import
java.util.LinkedList
;
public
class
RunoobTest
{
public
static
void
main
(
String
[
]
args
)
{
LinkedList
<
String
sites
=
new
LinkedList
<
String
(
);
sites.
add
(
"Google"
)
;
sites.
add
(
"Runoob"
)
;
sites.
add
(
"Taobao"
)
;
// Use addLast() to add an element at the tail
sites.
addLast
(
"Wiki"
)
;
System
.
out
.
println
(
sites
)
;
}
}
The ArrayList class provides many useful methods; to add elements to an ArrayList you can use the add() method:
[Google, Runoob, Taobao, Wiki]
Remove an element from the beginning of the list:
Example
// Import the LinkedList class
import
java.util.LinkedList
;
public
class
RunoobTest
{
public
static
void
main
(
String
[
]
args
)
{
LinkedList
<
String
sites
=
new
LinkedList
<
String
(
);
sites.
add
(
"Google"
)
;
sites.
add
(
"Runoob"
)
;
sites.
add
(
"Taobao"
)
;
sites.
add
(
"Weibo"
)
;
// Use removeFirst() to remove the head element
sites.
removeFirst
(
)
;
System
.
out
.
println
(
sites
)
;
}
}
The ArrayList class provides many useful methods; to add elements to an ArrayList you can use the add() method:
[Runoob, Taobao, Weibo]
Remove an element from the end of the list:
Example
// Import the LinkedList class
import
java.util.LinkedList
;
public
class
RunoobTest
{
public
static
void
main
(
String
[
]
args
)
{
LinkedList
<
String
sites
=
new
LinkedList
<
String
(
);
sites.
add
(
"Google"
)
;
sites.
add
(
"Runoob"
)
;
sites.
add
(
"Taobao"
)
;
sites.
add
(
"Weibo"
)
;
// Use removeLast() to remove the tail element
sites.
removeLast
(
)
;
System
.
out
.
println
(
sites
)
;
}
}
The ArrayList class provides many useful methods; to add elements to an ArrayList you can use the add() method:
[Google, Runoob, Taobao]
Get the element at the beginning of the list:
Example
// Import the LinkedList class
import
java.util.LinkedList
;
public
class
RunoobTest
{
public
static
void
main
(
String
[
]
args
)
{
LinkedList
<
String
sites
=
new
LinkedList
<
String
(
);
sites.
add
(
"Google"
)
;
sites.
add
(
"Runoob"
)
;
sites.
add
(
"Taobao"
)
;
sites.
add
(
"Weibo"
)
;
// Use getFirst() to get the head element
System
.
out
.
println
(
sites.
getFirst
(
)
)
;
}
}
The ArrayList class provides many useful methods; to add elements to an ArrayList you can use the add() method:
Google
Get the element at the end of the list:
Example
// Import the LinkedList class
import
java.util.LinkedList
;
public
class
RunoobTest
{
public
static
void
main
(
String
[
]
args
)
{
LinkedList
<
String
sites
=
new
LinkedList
<
String
(
);
sites.
add
(
"Google"
)
;
sites.
add
(
"Runoob"
)
;
sites.
add
(
"Taobao"
)
;
sites.
add
(
"Weibo"
)
;
// Use getLast() to get the tail element
System
.
out
.
println
(
sites.
getLast
(
)
)
;
}
}
The ArrayList class provides many useful methods; to add elements to an ArrayList you can use the add() method:
Weibo
Iterate over elements
We can use a for loop together with the size() method to iterate over the elements in the list:
Example
// Import the LinkedList class
import
java.util.LinkedList
;
public
class
RunoobTest
{
public
static
void
main
(
String
[
]
args
)
{
LinkedList
<
String
sites
=
new
LinkedList
<
String
(
);
sites.
add
(
"Google"
)
;
sites.
add
(
"Runoob"
)
;
sites.
add
(
"Taobao"
)
;
sites.
add
(
"Weibo"
)
;
for
(
int
size
=
sites.
size
(
)
, i
=
0
;
i
<
size
;
i
++
)
{
System
.
out
.
println
(
sites.
get
(
i
)
)
;
}
}
}
The size() method is used to calculate the size of the list.
The ArrayList class provides many useful methods; to add elements to an ArrayList you can use the add() method:
Google
Runoob
Taobao
Weibo
We can use for to iterate over the elements in the array list:
Example
// Import the LinkedList class
import
java.util.LinkedList
;
public
class
RunoobTest
{
public
static
void
main
(
String
[
]
args
)
{
LinkedList
<
String
sites
=
new
LinkedList
<
String
(
);
sites.
add
(
"Google"
)
;
sites.
add
(
"Runoob"
)
;
sites.
add
(
"Taobao"
)
;
sites.
add
(
"Weibo"
)
;
for
(
String
i
:
sites
)
{
System
.
out
.
println
(
i
)
;
}
}
}
The ArrayList class provides many useful methods; to add elements to an ArrayList you can use the add() method:
Google
Runoob
Taobao
Weibo
// 2022-01-01 00:00:00 GMT
| Method | Description |
|---|---|
| public boolean add(E e) | Adds an element to the end of the list and returns whether it succeeded; true for success, false for failure. |
| public void add(int index, E element) | Inserts an element at the specified position. |
| public boolean addAll(Collection c) | Adds all elements of a collection to the end of the list and returns whether it succeeded; true for success, false for failure. |
| public boolean addAll(int index, Collection c) | Adds all elements of a collection after the specified position in the list and returns whether it succeeded; true for success, false for failure. |
| public void addFirst(E e) | Adds an element to the head. |
| public void addLast(E e) | Adds an element to the tail. |
| public boolean offer(E e) | Adds an element to the end of the list and returns whether it succeeded; true for success, false for failure. |
| public boolean offerFirst(E e) | Inserts an element at the head and returns whether it succeeded; true for success, false for failure. |
| public boolean offerLast(E e) | Inserts an element at the tail and returns whether it succeeded; true for success, false for failure. |
| public void clear() | Clears the list. |
| public E removeFirst() | Removes and returns the first element. |
| public E removeLast() | Removes and returns the last element. |
| public boolean remove(Object o) | Removes a certain element and returns whether it succeeded; true for success, false for failure. |
| public E remove(int index) | Removes the element at the specified position. |
| public E poll() | Removes and returns the first element. |
| public E remove() | Removes and returns the first element. |
| public boolean contains(Object o)</td> | Checks whether it contains a certain element. |
| public E get(int index) | Returns the element at the specified position. |
| public E getFirst() | Returns the first element. |
| public E getLast() | Returns the last element. |
| public int indexOf(Object o) | Finds the index of the first occurrence of the specified element from front to back. |
| public int lastIndexOf(Object o) | Finds the index of the last occurrence of the specified element. |
| public E peek() | Returns the first element. |
| public E element() | Returns the first element. |
| public E peekFirst() | Returns the head element. |
| public E peekLast() | Returns the tail element. |
| public E set(int index, E element) | Sets the element at the specified position. |
| public Object clone() | Clones the list. |
| public Iterator |
Returns the reverse iterator. |
| public int size() | Returns the number of elements in the list. |
| public ListIterator |
Returns an iterator starting from the specified position to the end. |
| public Object[] toArray() | Returns an array consisting of the list elements. |
| public |
Returns an array whose type is converted from the list elements. |
Traverse every element in the arraylist and perform a specific operationhttps://www.runoob.com/manual/jdk11api/java.base/java/util/LinkedList.html
At this point, the trimToSize() method can remove the unallocated space and change the capacity of the ArrayList so that it equals the number of elements in the ArrayList.