来源:https://loliwithpick.github.io/deny.github.io/2019/03/15/linkedList/
链表的基本实现
class LinkList{
//存放头结点
Node head;
//记录数据长度
int size;
LinkList(){
head = new Node();
size = 0;
}
class Node{
public int value = -1;
public Node next = null;
Node(int value) {
this.value = value;
}
public Node(){}
}
/**
* 在链表头添加节点
* @param value
*/
public void addFirst(int value) {
final Node node = new Node(value);
final Node nextN = head.next;
node.next = nextN;
head.next = node;
size++;
}
/**
* 在链尾添加节点
* @param value
*/
public void addLast(int value) {
final Node node = new Node(value);
Node cur = head;
while (cur.next != null)
cur = cur.next;
cur.next = node;
size++;
}
/**
* 指定位置添加节点
* @param value
* @param index
*/
public void add(int value, int index) {
if (index > size)
return;
Node pre = null, cur = head;
final Node node = new Node(value);
for (int i = 0; i < index; i++) {
pre = cur;
cur = cur.next;
}
pre.next = node;
node.next = cur;
}
public int getSize() {
return size;
}
/**
* 删除链头节点并返回节点值
* @return
*/
public int removeFirst() {
if (size == 0)
return -1;
final Node node = head.next;
final Node nextN = node.next;
head.next = nextN;
size--;
return node.value;
}
/**
* 删除链尾节点并返回节点值
* @return
*/
public int removeLast(){
if (size == 0)
return -1;
Node cur = head;
while (cur.next != null && cur.next.next != null) {
cur = cur.next;
}
final Node node = cur.next;
cur.next = null;
size--;
return node.value;
}
反转链表
/**
* 反转链表
*/
public void reverse(){
if (size == 0)
return;
Node pre = null, cur = head.next;
while (cur != null) {
Node tmp = cur.next;
cur.next = pre;
pre = cur;
cur = tmp;
}
head.next = pre;
}
判断链表是否存在循环
/**
* 判断是否存在循环
*/
public boolean loopCheck(){
if (size == 0)
return false;
Node first = head.next;
Node second = first.next;
while (second != null && second.next != null){
if (first == second)
return true;
first = first.next;
second = second.next.next;
}
return false;
}
删除倒数第 k 个元素的两种方法
/**
* 删除倒数第 k 个数(方法一)
* @param k
* @return
*/
public int deleteDESC(int k) {
if (size == 0 || k > size)
return -1;
Node cur = head;
for (int i = 0; i < (size - k); i++) {
cur = cur.next;
}
final Node tmp = cur.next;
cur.next = tmp.next;
return tmp.value;
}
/**
* 删除倒数第 k 个数(方法二)
* @param k
* @return
*/
public int deleteLastKth(int k) {
if (size == 0 || k > size)
return -1;
Node first = head.next;
while (k > 1) {
first = first.next;
k--;
}
Node second = head.next, pre = head;
while (first.next != null) {
first = first.next;
pre = second;
second = second.next;
}
pre.next = second.next;
return second.value;
}
回文链表的判断
/**
* 判断是否回文链表
* @return
*/
public boolean palindrome(){
if (size <= 1)
return false;
Node first = head.next, second = head.next.next, left, right;
while (second.next != null && second.next.next != null) {
first = first.next;
second = second.next.next;
}
if (second.next != null) {
right = first.next.next;
left = reverse(first);
} else {
right = first.next;
left = reverse(first);
}
return ToF(left, right);
}
/**
* 反转以参数节点为尾的链表
* @param node
* @return
*/
public Node reverse(Node node) {
Node pre = null, cur = head.next;
while (cur != node) {
Node tmp = cur.next;
cur.next = pre;
pre = cur;
cur = tmp;
}
cur.next = pre;
return cur;
}
/**
* 判断两个链表所有的节点值是否相等
* @param left
* @param right
* @return
*/
public boolean ToF(Node left, Node right) {
while (left != null && right != null && left.value == right.value) {
left = left.next;
right = right.next;
}
if (left == null && right == null)
return true;
else
return false;
}
/**
* 输出所有节点值
*/
public void print() {
Node cur = head.next;
while (cur != null) {
System.out.print(cur.value+" ");
cur = cur.next;
}
}
}
以上均为本人学习时的笔录总结,若有错误之处,请多多指教。