1、介绍
链表是有序的列表,但是它在内存中是存储如下
链表是以节点的方式来存储,是链式存储
每个节点包含 data 域, next 域:指向下一个节点.
如图:发现链表的各个节点不一定是连续存储
链表分带头节点的链表和没有头节点的链表,根据实际的需求来确定
单链表(带头结点) 逻辑结构示意图如下
2、思路分析
1) 第一种方法在添加节点时,直接添加到链表的尾部
2) 第二种方式在添加节点时,根据编号将节点插入到指定位置(如果有这个编号,则添加失败,并给出提示)
3) 修改节点功能;(1) 先找到该节点,通过遍历,(2) 再修改节点信息 temp.name = newHeroNode.name ; temp.nickname= newHeroNode.nickname
4) 删除节点
3、代码实现
package com.czq.datastructure;
/**
* 单链表
*
* @author czq
* @date 2020/08/12
*/
public class SingleLinkedList {
//先初始化一个头节点, 头节点不要动, 不存放具体的数据
private HeroNode head = new HeroNode(0, "", "");
//返回头节点
public HeroNode getHead() {
return head;
}
//添加节点,不考虑编号顺序
//1. 找到当前链表的最后节点,2. 将最后这个节点的 next 指向新的节点
public void add(HeroNode heroNode) {
//因为 head 节点不能动,因此我们需要一个辅助遍历 temp
HeroNode temp = head;
//遍历链表,找到最后
while (temp.next != null) {
//如果没有找到最后,将 temp 后移
temp = temp.next;
}
//当退出 while 循环时,temp 就指向了链表的最后,将最后这个节点的 next 指向新的节点
temp.next = heroNode;
}
//添加节点,要考虑编号顺序,根据编号将节点插入到指定位置
//1. 找到当前链表的最后节点,2. 判断是否有这个编号节点,3.将这个节点的插入到找到的这个位置
public void addByOrder(HeroNode heroNode) {
//辅助变量 temp
HeroNode temp = head;
// flag 标志添加的编号是否存在
boolean flag = false;
while (temp.next != null) {
//位置找到,就在 temp 的后面插入
if (temp.next.no > heroNode.no) {
break;
} else if (temp.next.no == heroNode.no) {
//说明需要添加的 heroNode 的编号已然存在
flag = true;
break;
}
//后移,遍历当前链表
temp = temp.next;
}
//判断 flag 的值,编号是否存在
if (flag) {
System.out.printf("准备插入的英雄的编号 %d 已经存在了, 不能加入\n", heroNode.no);
} else {
//插入到链表中, temp 的后面
heroNode.next = temp.next;
temp.next = heroNode;
}
}
//删除节点,在比较时,是 temp.next.no 和需要删除的节点的 no 比较,需要找到待删除的前一个节点
public void del(int no) {
if (head.next == null) {
System.out.println("链表为空");
return;
}
//辅助变量 temp
HeroNode temp = head;
// 标志是否找到待删除节点的
boolean flag = false;
while (temp.next != null) {
//找到的待删除节点的前一个节点 temp
if (temp.next.no == no) {
flag = true;
break;
}
//temp 后移,遍历
temp = temp.next;
}
//判断 flag,是否找到需要删除的节点
if (flag) {
//可以删除
temp.next = temp.next.next;
} else {
System.out.printf("要删除的 %d 节点不存在\n", no);
}
}
//修改节点, 根据 no 编号来修改,即 no 编号不能改
public void update(HeroNode newHeroNode) {
if (head.next == null) {
System.out.println("链表为空");
return;
}
//辅助变量 temp
HeroNode temp = head.next;
//表示是否找到该节点
boolean flag = false;
while (temp != null) {
//找到对应no的节点
if (temp.no == newHeroNode.no) {
flag = true;
break;
}
//temp 后移,遍历
temp = temp.next;
}
//根据 flag 判断是否找到要修改的节点
if (flag) {
temp.name = newHeroNode.name;
temp.nickname = newHeroNode.nickname;
} else {
System.out.printf("没有找到 编号 %d 的节点,不能修改\n", newHeroNode.no);
}
}
//遍历链表
public void list() {
//判断链表是否为空
if (head.next == null) {
System.out.println("链表为空");
return;
}
//因为头节点,不能动,因此需要一个辅助变量来遍历
HeroNode temp = head.next;
while (temp != null) {
//输出节点的信息
System.out.println(temp);
//将 temp 后移
temp = temp.next;
}
}
}
//定义 HeroNode,每个 HeroNode 对象就是一个节点
class HeroNode {
public int no;
public String name;
public String nickname;
//指向下一个节点
public HeroNode next;
public HeroNode(int no, String name, String nickname) {
this.no = no;
this.name = name;
this.nickname = nickname;
}
//为了显示打印节点,重写 toString
@Override
public String toString() {
return "HeroNode [no=" + no + ", name=" + name + ", nickname=" + nickname + "]";
}
}
4、常见问题
1) 求单链表中有效节点的个数
/**
* 获取到单链表的节点的个数(如果是带头结点的链表,需求不统计头节点)
*
* @param head 链表的头节点
* @return 返回的就是有效节点的个数
*/
public static int getLength(HeroNode head) {
//空链表
if (head.next == null) {
return 0;
}
int length = 0;
//定义一个辅助的变量, 没有统计头节点
HeroNode cur = head.next;
while (cur != null) {
length++;
//遍历
cur = cur.next;
}
return length;
}
2) 查找单链表中的倒数第 k 个结点 【新浪面试题】
/**
* 查找单链表中的倒数第 k 个结点 【新浪面试题】
* 1. 先把链表从头到尾遍历,得到链表的总的长度 getLength
* 2. 得到 size 后,我们从链表的第一个开始遍历 (size-index)个,就可以得到
* 5. 如果找到了,则返回该节点,否则返回 null
*
* @param head 链表的头节点
* @param index 倒数第 index 个
* @return 返回倒数第 k 个结点
*/
public static HeroNode findLastIndexNode(HeroNode head, int index) {
//判断如果链表为空,返回 null
if (head.next == null) {
//没有找到
return null;
}
//第一个遍历得到链表的长度(节点个数)
int size = getLength(head);
if (index <= 0 || index > size) {
return null;
}
//定义辅助变量,从头结点的下一个节点开始
HeroNode cur = head.next;
//第二次遍历到 size-index 位置,就是我们倒数的第 K 个节点
for (int i = 0; i < size - index; i++) {
cur = cur.next;
}
return cur;
}
3) 单链表的反转【腾讯面试题】
/**
* 单链表反转
* 1. 定义一个新的头结点 reverseHead,用来临时接收原链表遍历的节点
* 2. 遍历原来的链表,每遍历一个节点就插入到 reverseHead 的next位置
* 3. 将 head.next 指向 reverseHead.next
*
* @param head 链表的头节点
*/
public static void reversetList(HeroNode head) {
//如果当前链表为空,或者只有一个节点,无需反转,直接返回
if (head.next == null || head.next.next == null) {
return;
}
//定义一个辅助的变量,用于遍历
HeroNode cur = head.next;
//指向当前节点[cur]的下一个节点
HeroNode next = null;
//定义一个新的头节点,临时接收原链表遍历的节点
HeroNode reverseHead = new HeroNode(0, "", "");
//遍历原来的链表,每遍历一个节点,就将其取出,并放在新的链表 reverseHead 的最前端
while (cur != null) {
//先暂时保存当前节点的下一个节点,后面需要使用
next = cur.next;
//将 cur 的下一个节点指向新的链表的最前端
cur.next = reverseHead.next;
//将 cur 连接到新的链表上
reverseHead.next = cur;
// cur 后移
cur = next;
}
//while结束时,reverseHead链表就是head的反转链表,将 head.next 指向 reverseHead.next,实现单链表的反转
head.next = reverseHead.next;
}
4) 从尾到头打印单链表 【方式 1:反向遍历 。 方式 2:Stack 栈】
/**
* 从尾到头打印单链表 【方式 1:反向遍历; 方式 2:Stack 栈】
*
* @param head 链表的头节点
*/
public static void printStack(HeroNode head) {
//空链表,不能打印
if (head.next == null) {
return;
}
//创建一个栈,将各个节点压入栈
Stack<HeroNode> stack = new Stack();
//定义一个辅助的变量,用于遍历
HeroNode cur = head.next;
//将链表的所有节点压入栈
while (cur != null) {
//入栈
stack.push(cur);
// cur 后移
cur = cur.next;
}
//出栈
while (stack.size() > 0) {
// pop 就是将栈顶的数据取出
System.out.println(stack.pop());
}
}
5) 合并两个有序的单链表,合并之后的链表依然有序
/**
* 合并两个有序的单链表,合并之后的链表依然有序
*
* @param head1 有序链表的头节点1
* @param head2 有序链表的头节点2
* @return 返回合并后的有序链表
*/
public static HeroNode combineList(HeroNode head1, HeroNode head2) {
//一条链表为空,直接返回另一条链表
if (head1.next == null) {
return head2;
} else if (head2.next == null) {
return head1;
}
HeroNode newHead = new HeroNode(0, "", "");
//遍历的临时指针变量
HeroNode temp1 = head1.next;
HeroNode temp2 = head2.next;
//新链表需从头部开始添加
HeroNode temp3 = newHead;
while (temp1 != null && temp2 != null) {
if (temp1.no <= temp2.no) {
temp3.next = temp1;
temp1 = temp1.next;
} else {
temp3.next = temp2;
temp2 = temp2.next;
}
//新链表指针后移
temp3 = temp3.next;
}
//找到没有遍历完的链表,并直接加入到新链表后面
if (temp1 != null) {
temp3.next = temp1;
}
if (temp2 != null) {
temp3.next = temp2;
}
return newHead;
}