实现单向链表的遍历、查找、插入、删除
节点Node类
class Node{
Integer data;
Node next;
public Node(int data, Node next){
this.data = data;
this.next = next;
}
public Node() {
}
}
链表操作:
public class LinkedList {
private Node head = null;
/**
* 遍历链表
* 我想的是:结点的值不为null,就输出
* 但是:结点不为null就行呀
* @param head
*/
public void travel(Node head){
Node p = head;
while(p != null){
System.out.println(p.data);
p = p.next;
}
}
/**
* 查找链表中某值,没有,则返回null
* 1、如果链表为空,则返回null
* 2、链表不为空,遍历每一个节点,当值==target,则返回该节点,否则继续下个节点
* 遍历完仍然没有找到target,则返回null
* @param target
* @return
*/
public Node find(int target){
Node p = head;
if(head == null){
return null;
}
while (p != null){
if(p.data == target){
return p;
}else{
p = p.next;
}
}
return null;
}
/**
* 链表头部插入
* 如果链表为空,则新节点 就是head。
* @param value
*/
public void insertHead(int value){
Node p = new Node(value,null);
if(head == null){
head = p;
}else{
p.next = head;
head = p;
}
}
//看代码后想了一下,不管链表是不是空,新节点一定是head。所以上述可以优化
//优化后代码
public void insertHead1(int value){
Node p = new Node(value,null);
p.next = head;
head = p;
}
/**
* 在链表尾部插入元素,就是遍历链表到尾部,然后新增节点
* @param value
*/
public void insertTail(int value){
Node node = new Node(value,null);
//如果链表为空
if(head == null){
head = node;
}else{
//链表不为空
Node p = head;
//当p不是最后一个结点,一直遍历,直到最后一个结点
while(p.next != null){
p = p.next;
}
p.next = node;
}
}
Node tail = null;
//优化1 增加tail指针:记录链表尾节点的位置
//思路:如果链表为空,则tail和head都指向新节点。
//如果链表不为空,每次向尾部插入数据,就是tail(最后一个节点)指向新节点,然后将node置为尾节点tail
public void insertTail1(int value){
Node node = new Node(value,null);
if(head == null){
head = node;
tail = node;
}else{
tail.next = node;
tail = node;
}
}
//优化2,增加虚拟头节点
public void insertTail2(int value){
Node node = new Node(value,null);
}
/**
* 在给定节点之后插入
* 如果指定节点p为null,则直接返回
* 指定节点p不为null,先让node指向p.next(p的下个节点),然后再让指定节点p指向node
* @param p
* @param value
*/
public void insertAfter(Node p, int value){
if(p == null){
return;
}
Node node = new Node(value, null);
node.next = p.next;
p.next = node;
}
/**
* 删除:删除给定节点之后的节点
*/
public void delete(Node target){
if(target == null){
//给定节点为空,不进行任何操作
}
if(target.next == null){
//就是说指定节点是尾节点,那不用进行任何操作
}
//否则:
target.next = target.next.next;
//所以,优化总结:
if(target == null || target.next == null){
return;
}
//否则:p的next节点,是p的next的next节点
target.next = target.next.next;
}
/**
* 删除给定节点
* 找不到指定节点p的前一个节点,怎么办
* @param target
*/
public void delete1(Node target){
//如果链表为空,或指定节点为空,不做处理,直接结束
if(head == null || target == null){
return;
}
//如果target是头节点
if(target == head){
head = head.next;
}
Node p = head;
//定义目标节点target的前一个节点pre
Node pre = null;
//先去遍历,找到给定节点target,及它的前一个节点pre
while(p.next != target){
//遍历到最后一个节点,仍然没有找到目标节点target,直接结束
if(p.next == null){
return;
}
p = p.next;
}
//找到了
pre = p;
//删除节点
pre.next = target.next;
}
/**
* 删除给定节点
* 思路:先遍历链表,去找指定节点,遍历完整个链表成后,看它的值是不是null
* 如果是null,说明没有目标值,直接返回
* @param target
*/
public void delete2(Node target){
if(head == null || target == null){
return;
}
Node pre = null;
Node p = head;
//遍历链表,不到最后一个节点,一直遍历
while(p != null){
//当找到指定节点target,则跳出循环
if(p == target){
break;
}
pre = p;
p = p.next;
}
//没有找到目标节点
if(p == null){
return;
//目标节点是头节点,这里的条件也可以是:target == head
}else if(pre == null){
//删除头节点
head = head.next;
}else{
pre.next = pre.next.next;
}
}
/**
* 删除给定节点-优化:添加虚拟头节点
* @param target
*/
public void delete3(Node target){
if(head == null || target == null){
return;
}
Node newHead = new Node();
newHead.next = head;
Node pre = newHead;
Node node = head;
while(node != null){
if(node == target){
break;
}
pre = node;
node = node.next;
}
if(node == null){
return;
}
pre.next = pre.next.next;
}
}
链表尾部插入优化2:引入虚拟头节点
/**
* 引入虚拟头节点,目的是:简化编程,统一处理逻辑
* 虚拟头节点,指向真正的头节点,这样即使真正头节点为空,也能统一处理
*/
public class LinkedList {
//虚拟头节点
private Node head = new Node();
//尾节点
private Node tail = head;
/**
* 尾部插入节点时,尾节点tail指向新增节点,然后将node置为尾节点tail
* @param value
*/
public void insertTail2(int value){
Node node = new Node(value,null);
tail.next = node;
tail = node;
}
}