public class LinkedList {
private Node root = null;
int size = 0;
public LinkedList() {
}
// 数组创建一个链表
public static Node createLinkedLNode(int[] array) {
// 创建一个根节点
Node root = null;
// 从末尾元素开始依次创建Node节点
for (int i = array.length - 1; i >= 0; i--) {
Node node = new Node(array[i], null);
// 创建两个节点的连接关系
if (root != null) {
node.setNext(root);
root = node;
} else {
root = node;
}
}
return root;
}
public LinkedList(int[] array) {
this.root = LinkedList.createLinkedLNode(array);
this.size = array.length;
}
// 获取长度
public int size() {
return this.size;
}
// 获取某个索引节点内容
public int get(int index) {
int i = 0;
Node node = this.root;
// 依次往前推进
while (i < index) {
node = node.getNext();
i++;
}
return node.getContent();
}
// 查找某个值的索引值,默认不存在为-1
public int find(int value) {
Node node = this.root;
// index 保存当前的索引
int index = 0;
// 依次遍历链表,找到内容等于value的node,返回index
while (node != null) {
if (node.getContent() == value) {
return index;
}
node = node.getNext();
index++;
}
return -1;
}
// 末尾添加元素
public boolean add(int value) {
return this.add(this.size - 1, value);
}
// 头部插入元素
public boolean addFirst(int value) {
return this.add(-1, value);
}
// 插入元素
public boolean add(int index, int value) {
if (index < -1 || index > this.size - 1){
return false;
}
if (index == -1) {
if (this.root != null) {
this.root = new Node(value, this.root);
} else {
this.root = new Node(value, null);
}
} else {
Node pre = this.root;
while (index > 0) {
if (pre.getNext() == null) {
return false;
}
pre = pre.getNext();
index--;
}
if (pre == null) {
return false;
}
Node newNode = new Node(value, pre.getNext());
pre.setNext(newNode);
}
this.size++;
return true;
}
// 删除最后一个元素
public boolean removeLast() {
return this.remove(this.size - 1);
}
// 删除第一个元素
public boolean removeFirst() {
return this.remove(0);
}
// 删除元素
// index = 0, 表示删除第 1 个元素
// index = n,表示删除第 n+1 个元素
public boolean remove(int index) {
if (index < 0 || index > this.size - 1){
return false;
}
// 判断整个列表为空情况,删除失败
if (this.root == null) {
return false;
}
if (index == 0) {
// 删除第一个元素,必须先保留 this.root,因为紧接着 this.root 会被修改
Node node = this.root;
this.root = node.getNext();
node.setNext(null);
} else {
// 判断越界问题
if(this.size < index + 1){
return false;
}
// 遍历获取index的前置节点
Node pre = this.root;
while (index > 1) {
pre = pre.getNext();
index--;
}
// 修改next指针,需要先保存 pre.next,因为紧接着下一步会修改 pre.next 指针
Node next = pre.getNext();
pre.setNext(next.getNext());
next.setNext(null);
}
this.size--;
return true;
}
// 链表中的元素字符串形式
public String toString() {
if (this.root == null) {
return "";
}
StringBuilder str = new StringBuilder();
Node node = this.root;
while (node != null) {
str.append(node.getContent()).append(" ");
node = node.getNext();
}
return str.toString();
}
public static void main(String[] args) {
int[] array = {
10, 9, 2, 4};
LinkedList linkedList = new LinkedList(array);
}
}
链表类
public class Node {
private int content;
private Node next;
public Node() {
}
public Node(int content, Node next) {
this.content = content;
this.next = next;
}
public int getContent() {
return content;
}
public void setContent(int content) {
this.content = content;
}
public Node getNext() {
return next;
}
public void setNext(Node next) {
this.next = next;
}
}