实现双向链表MyLinkedList
一、MyLinkedList初始化
class Node{
int val;
Node prev = null;
Node next = null;
public Node(int val){
this.val = val;
}
}
public class MyLinkedList {
private Node head;
private Node tail;
private int length = 0;
public int length(){
return this.length;
}
public MyLinkedList(){
head = null;
tail = null;
}
二、增
2.1 头插
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/6745fce27eaccacd72277361e26fba32.png)
public void addFirst(int val){
Node newNode = new Node(val);
if(head == null){
head = newNode;
tail = newNode;
length++;
return;
}
newNode.next = head;
head.prev = newNode;
head = newNode;
length++;
return;
}
2.2 尾插