2024-07-12
한어Русский языкEnglishFrançaisIndonesianSanskrit日本語DeutschPortuguêsΕλληνικάespañolItalianoSuomalainenLatina
When inserting or deleting an element at any position in ArrayList, it is necessary to move the subsequent elements forward or backward as a whole, which takes O(n) time and is inefficient. Therefore, ArrayList is not suitable for scenarios where there are many insertions and deletions at any position. Therefore, LinkedList, a linked list structure, is introduced in Java collections.
2.1 The concept and structure of linked lists
A linked list is a non-continuous storage structure in a physical storage structure. The logical order of data elements is realized through the order of reference links in the linked list.
2.2 Implementation of linked lists
1. Head insertion method addFirst()
- public void addFirst(int data) {
- ListNode node = new ListNode(data);
- node.next = head;
- head = node;
- }
2. Tail insertion method addLast()
- public void addList(int data) {
- ListNode node = new ListNode(data);
- if (head == null) {
- head = node;
- }
- ListNode cur = head;
- while (cur.next != null) {
- cur = cur.next;
- }
- cur.next = node;
- }
3.addIndex()
- public void addIndex(int index, int data) {
- int len = size();
- // 不合法
- if (index < 0 || index > len) {
- System.out.println("index不合法");
- return;
- }
- // 空链表
- if (index == 0) {
- addFirst(data);
- return;
- }
- // 尾部插入
- if (index == len) {
- addLast(data);
- return;
- }
-
- //中间位置插入
- ListNode cur = head;
- while (index - 1 != 0) {
- cur = cur.next;
- index--;
- }
- ListNode node = new ListNode(data);
- node.next = cur.next;
- cur.next = node;
- }
4.contains()
- public boolean contains(int key) {
- ListNode cur = head;
- while (cur != null) {
- if (cur.val == key) {
- return true;
- }
- cur = cur.next;
- }
- return false;
- }
5.remove()
- public void remove(int key) {
- if (head == null) {
- return;
- }
- if (head.val == key) {
- head = head.next;
- }
-
- ListNode cur = findNodeOfKey(key);
- if (cur == null) {
- return;
- }
- ListNode del = cur.next;
- cur.next = del.next;
- }
-
- private ListNode findNodeOfKey(int key) {
- ListNode cur = head;
- while (cur.next != null) {
- if (cur.next.val == key) {
- return cur;
- }
- cur = cur.next;
- }
- return null;
- }
6.removeAllKey()
- public void removeAllKey(int key) {
- ListNode cur = head.next;
- ListNode prev = head;
- while (cur != null) {
- if (cur.val == key) {
- prev.next = cur.next;
- } else {
- prev = cur;
- }
- cur = cur.next;
- }
- if (head.val == key) {
- head = head.next;
- }
- }
7.clear()
- public void clear() {
- ListNode cur = head;
- while (cur != null) {
- ListNode curNext = cur.next;
- cur.next = null;
- cur = curNext;
- }
- head = null;
- }
8.size()
- public int size() {
- int count = 0;
- ListNode cur = head;
- while (cur != null) {
- count++;
- cur = cur.next;
- }
- return count;
- }
9.display()
- public void display() {
- ListNode cur = head;
- while (cur != null) {
- System.out.print(cur.val + " ");
- cur = cur.next;
- }
- }
- / 2、无头双向链表实现
- public class MyLinkedList {
- //头插法
- public void addFirst(int data){ }
- //尾插法
- public void addLast(int data){}
- //任意位置插入,第一个数据节点为0号下标
- public void addIndex(int index,int data){}
- //查找是否包含关键字key是否在单链表当中
- public boolean contains(int key){}
- //删除第一次出现关键字为key的节点
- public void remove(int key){}
- //删除所有值为key的节点
- public void removeAllKey(int key){}
- //得到单链表的长度
- public int size(){}
- public void display(){}
- public void clear(){}
- }
- package DounlyLinkedList;
-
- import java.util.LinkedList;
- import java.util.List;
-
- public class MyDoublyLinkedList implements IList {
-
- static class ListNode {
- public int val;
- public ListNode next;
- public ListNode prev;
-
- public ListNode(int val) {
- this.val = val;
- }
- }
-
- public ListNode head;
- public ListNode last;
-
-
- @Override
- public void addFist(int data) {
- ListNode node = new ListNode(data);
- if (head == null) {
- head = node;
- last = node;
- } else
- node.next = head;
- head.prev = node;
- head = node;
- }
-
- @Override
- public void addLast(int data) {
- ListNode node = new ListNode(data);
- if (head == null) {
- head = node;
- last = node;
- }
- last.next = node;
- node.prev = last;
- last = node;
- }
-
- @Override
- public void addIndex(int index, int data) {
- int len = size();
- if (index < 0 || index > len) {
- System.out.println("index 不合法!");
- }
-
- if (index == 0) {
- addFist(data);
- return;
- }
-
- if (index == len) {
- addLast(data);
- return;
- }
- ListNode cur = findIndex(index);
- ListNode node = new ListNode(data);
- node.next = cur;
- cur.prev.next = node;
- node.prev = cur.prev;
- cur.prev = node;
- }
-
- private ListNode findIndex(int index) {
- ListNode cur = head;
- while (index != 0) {
- cur = cur.next;
- index--;
- }
- return cur;
- }
-
-
- @Override
- public boolean contains(int key) {
- ListNode cur = head;
- int count = 0;
- while (cur != null) {
- if (cur.val == key) {
- return true;
- }
- cur = cur.next;
- }
- return false;
- }
-
- @Override
- public void remove(int key) {
- ListNode cur = head;
- while (cur != null) {
- if (cur.val == key) {
- if (cur == head) {
- head = head.next;
- if (head != null) {
- head.prev = null;
- }
- head.prev = null;
- } else {
- cur.prev.next = cur.next;
- if (cur.next == null) {
- last = last.prev;
- } else {
- cur.next.next = cur.prev;
- }
- }
- return;
- }
- cur = cur.next;
-
- }
- }
-
- @Override
- public void removeAllKey(int key) {
- ListNode cur = head;
- while (cur != null) {
- if (cur.val == key) {
- if (cur == head) {
- head = head.next;
- if (head != null) {
- head.prev = null;
- }
- head.prev = null;
- } else {
- cur.prev.next = cur.next;
- if (cur.next == null) {
- last = last.prev;
- } else {
- cur.next.next = cur.prev;
- }
- }
- }
- cur = cur.next;
- }
- }
-
- @Override
- public int size() {
- ListNode cur = head;
- int count = 0;
- while (cur != null) {
- count++;
- cur = cur.next;
- }
- return count;
- }
-
- @Override
- public void clear() {
- ListNode cur = head;
- while (cur != null) {
- ListNode curNext = cur.next;
- cur.next = null;
- cur.prev = null;
- cur = curNext;
- }
- head = null;
- last = null;
- }
-
- @Override
- public void dispaly() {
- ListNode cur = head;
- while (cur != null) {
- System.out.print(cur.val + " ");
- cur = cur.next;
- }
- System.out.println();
- }
- }
-
-
-
-
-
- package DounlyLinkedList;
-
- public interface IList {
- void addFist(int data);
-
- void addLast(int data);
-
- void addIndex(int index, int data);
-
- boolean contains(int key);
-
- void remove(int key);
-
- void removeAllKey(int key);
-
- int size();
-
- void clear();
-
- void dispaly();
- }
4.1 What is LinkedList
【illustrate】
1. LinkedList implements the List interface
2. The underlying layer of LinkedList uses a bidirectional linked list
3. LinkedList does not implement the RandomAccess interface, so LinkedList does not support random access
4. Inserting and deleting elements at any position in LinkedList is more efficient, with a time complexity of O(1)
5. LinkedList is more suitable for inserting at any position
4.2 Use of LinkedList
1. Construction of LinkedList
method | explain |
LinkedList() | No parameter construction |
public LinkedList(Collection<?extends<Ec) | Construct a List using elements from other collection containers |
- import java.util.LinkedList;
- import java.util.List;
-
- public class Test {
- public static void main(String[] args) {
- List<Integer> list1 = new LinkedList<>();
- List<String> list2 = new java.util.ArrayList<>();
-
- list2.add("javaSE");
- list2.add("javaWeb");
- list2.add("javaEE");
-
- List<String> list3 = new LinkedList<>(list2);
-
- }
- }
-
-
2. Introduction to other common methods of LinkedList
method | explain |
boolean add(E e) | Tail plug e |
void add(int index, E element) | Insert e at index position |
boolean addAll(Collection c) | End insert element in c |
E remove(int index) | Delete the element at position index |
boolean remove(Object o) | Delete the first o encountered |
E get(int index) | Get the element at index position |
E set(int index, E element) | Set the element at index to element |
void clear() | Clear |
boolean contains(Object o) | Determine whether o is in the linear table |
int indexOf(Object o) | Returns the index of the first o |
int lastIndexOf(Object o) | Returns the last index of o |
List subList(int fromIndex, int toIndex) | Extract part of the list |
- public static void main(String[] args) {
- LinkedList<Integer> list = new LinkedList<>();
- list.add(1); // add(elem): 表示尾插
- list.add(2);
- list.add(3);
- list.add(4);
- list.add(5);
- list.add(6);
- list.add(7);
- System.out.println(list.size());
- System.out.println(list);
- // 在起始位置插入0
- list.add(0, 0); // add(index, elem): 在index位置插入元素elem
- System.out.println(list);
- list.remove(); // remove(): 删除第一个元素,内部调用的是removeFirst()
- list.removeFirst(); // removeFirst(): 删除第一个元素
- list.removeLast(); // removeLast(): 删除最后元素
- list.remove(1); // remove(index): 删除index位置的元素
- System.out.println(list);
- // contains(elem): 检测elem元素是否存在,如果存在返回true,否则返回false
- if(!list.contains(1)){
- list.add(0, 1);
- }
- list.add(1);
- System.out.println(list);
- System.out.println(list.indexOf(1)); // indexOf(elem): 从前往后找到第一个elem的位置
- System.out.println(list.lastIndexOf(1)); // lastIndexOf(elem): 从后往前找第一个1的位置
- int elem = list.get(0); // get(index): 获取指定位置元素
- list.set(0, 100); // set(index, elem): 将index位置的元素设置为elem
- System.out.println(list);
- // subList(from, to): 用list中[from, to)之间的元素构造一个新的LinkedList返回
- List<Integer> copy = list.subList(0, 3);
- System.out.println(list);
- System.out.println(copy);
- list.clear(); // 将list中元素清空
- System.out.println(list.size());
- }
3. LinkedList traversal
- public static void main(String[] args) {
- LinkedList<Integer> list = new LinkedList<>();
- list.add(1); // add(elem): 表示尾插
- list.add(2);
- list.add(3);
- list.add(4);
- list.add(5);
- list.add(6);
- list.add(7);
- System.out.println(list.size());
- // foreach遍历
- for (int e:list) {
- System.out.print(e + " ");
- }
- System.out.println();
- // 使用迭代器遍历---正向遍历
- ListIterator<Integer> it = list.listIterator();
- while(it.hasNext()){
- System.out.print(it.next()+ " ");
- }
- System.out.println();
- // 使用反向迭代器---反向遍历
- ListIterator<Integer> rit = list.listIterator(list.size());
- while (rit.hasPrevious()){
- System.out.print(rit.previous() +" ");
- }
- System.out.println();
- }
difference | ArrayList | LinkedList |
Storage space | Physically continuous | Logically continuous, but not necessarily physically continuous |
Random Access | Support O(1) | Not supported: O(N) |
Head Plug | Need to move elements, low efficiency O(N) | Just modify the reference point, the time complexity is O(1) |
insert | When space is insufficient, expansion is required | No concept of capacity |
Application Scenario | Efficient storage + frequent access of elements | Frequent insertion and deletion at any position |