- 单链表面试题
- 1. 单链表中有效节点的个数
- 1.1 思路
- 1.2 代码实现
- 2. 查找单链表中的倒数第k个节点
- 2.1 思路
- 2.2 代码实现
- 3. 单链表的反转
- 3.1 思路
- 3.2 代码实现
- 4. 从尾到头打印单链表
- 4.1 思路
- 4.2 代码实现
- 5. 合并两个有序的单链表,合并后依然有序
- 5.1 思路
- 5.2 实现
单链表面试题
以下实现的方法都是写在SingleLinkedListDemo类里的一个静态方法
循环遍历单链表,用一个变量 length 记录节点个数,节点不为空 length+1
1.2 代码实现package com.SingleLinkedListPrac04;
public class SingleLinkedListDemo {
public static void main(String[] args) {
HeroNode heroNode1 = new HeroNode(1, "宋江", "及时雨");
HeroNode heroNode2 = new HeroNode(2, "晁盖", "玉麒麟");
HeroNode heroNode3 = new HeroNode(3, "吴用", "智多星");
SingleLinkedList singleLinkedList = new SingleLinkedList();
singleLinkedList.add(heroNode1);
singleLinkedList.add(heroNode2);
singleLinkedList.add(heroNode3);
singleLinkedList.list();
//有效节点的个数测试
System.out.println("有效节点的个数为:" + getLength(singleLinkedList.getHead()));
}
// 9. 单链表中有效节点的个数
public static int getLength(HeroNode head) {
int length = 0;
HeroNode temp = head;
while (temp.next != null) {
length++;
temp = temp.next;
}
return length;
}
// 10. 查找单链表中的倒数第k个节点
// 11. 单链表的反转
// 12. 从尾到头打印单链表
// 13. 合并两个有序的单链表,合并后依然有序
}
class HeroNode {
// 1. 创建一个节点类,用来存放节点
// 2. 添加节点类的属性
public int no;
public String name;
public String nickname;
public HeroNode next;
// 3. 添加构造方法,初始化属性
public HeroNode(int no, String name, String nickname) {
this.no = no;
this.name = name;
this.nickname = nickname;
}
// 4. toString方法,显示节点信息
@Override
public String toString() {
return "HeroNode{" +
"no=" + no +
", name='" + name + ''' +
", nickname='" + nickname + ''' +
'}';
}
}
class SingleLinkedList {
// 5. 创建一个单链表的类,用来添加对单链表的操作
// 6. 添加节点
private HeroNode head = new HeroNode(0, "", "");
public HeroNode getHead() {
return this.head;
}
public void add(HeroNode heroNode) {
HeroNode temp = head;
while (temp.next != null) {
temp = temp.next;
}
temp.next = heroNode;
}
// 7. 有序添加节点
public void addByOrder(HeroNode heroNode) {
HeroNode temp = head;
while (temp.next != null) {
if (temp.next.no < heroNode.no) {
temp = temp.next;
}else if (temp.next.no > heroNode.no) {
heroNode.next = temp.next;
break;
}else {
System.out.printf("编号为 %d 的节点已经存在,添加失败~~~n", heroNode.no);
return;
}
temp.next = heroNode;
}
}
// 8. 显示单链表的节点信息
public void list() {
if (head.next == null) {
System.out.println("该单链表为空,没有可以显示的节点信息~~~");
return;
}
HeroNode temp = head;
while (temp.next != null) {
System.out.println(temp.next);
temp = temp.next;
}
}
}
2. 查找单链表中的倒数第k个节点
- 单链表的的查找我们一般是顺序查找的,这里要求倒着查找第 k 个节点
- 可以先获取单链表的长度 length 后,顺序查找 length-k 个节点
package com.SingleLinkedListPrac04;
public class SingleLinkedListDemo {
public static void main(String[] args) {
HeroNode heroNode1 = new HeroNode(1, "宋江", "及时雨");
HeroNode heroNode2 = new HeroNode(2, "晁盖", "玉麒麟");
HeroNode heroNode3 = new HeroNode(3, "吴用", "智多星");
SingleLinkedList singleLinkedList = new SingleLinkedList();
singleLinkedList.add(heroNode1);
singleLinkedList.add(heroNode2);
singleLinkedList.add(heroNode3);
singleLinkedList.list();
//有效节点的个数测试
System.out.println("有效节点的个数为:" + getLength(singleLinkedList.getHead()));
//单链表中的倒数第k个节点测试
HeroNode res = findLastIndexNode(singleLinkedList.getHead(), 1);
System.out.println("res = " + res);
}
// 9. 单链表中有效节点的个数
public static int getLength(HeroNode head) {
if (head.next == null) {
return 0;
}
int length = 0;
HeroNode temp = head;
while (temp.next != null) {
length++;
temp = temp.next;
}
return length;
}
// 10. 查找单链表中的倒数第k个节点
public static HeroNode findLastIndexNode(HeroNode head, int k) {
if (head.next == null) {
return null;
}
int length = getLength(head);
if (k <= 0 || k > length) {
return null;
}
HeroNode cur = head.next;
for (int i = 0; i < (length - k); i++) {
cur = cur.next;
}
return cur;
}
// 11. 单链表的反转
// 12. 从尾到头打印单链表
// 13. 合并两个有序的单链表,合并后依然有序
}
class HeroNode {
// 1. 创建一个节点类,用来存放节点
// 2. 添加节点类的属性
public int no;
public String name;
public String nickname;
public HeroNode next;
// 3. 添加构造方法,初始化属性
public HeroNode(int no, String name, String nickname) {
this.no = no;
this.name = name;
this.nickname = nickname;
}
// 4. toString方法,显示节点信息
@Override
public String toString() {
return "HeroNode{" +
"no=" + no +
", name='" + name + ''' +
", nickname='" + nickname + ''' +
'}';
}
}
class SingleLinkedList {
// 5. 创建一个单链表的类,用来添加对单链表的操作
// 6. 添加节点
private HeroNode head = new HeroNode(0, "", "");
public HeroNode getHead() {
return this.head;
}
public void add(HeroNode heroNode) {
HeroNode temp = head;
while (temp.next != null) {
temp = temp.next;
}
temp.next = heroNode;
}
// 7. 有序添加节点
public void addByOrder(HeroNode heroNode) {
HeroNode temp = head;
while (temp.next != null) {
if (temp.next.no < heroNode.no) {
temp = temp.next;
}else if (temp.next.no > heroNode.no) {
heroNode.next = temp.next;
break;
}else {
System.out.printf("编号为 %d 的节点已经存在,添加失败~~~n", heroNode.no);
return;
}
temp.next = heroNode;
}
}
// 8. 显示单链表的节点信息
public void list() {
if (head.next == null) {
System.out.println("该单链表为空,没有可以显示的节点信息~~~");
return;
}
HeroNode temp = head;
while (temp.next != null) {
System.out.println(temp.next);
temp = temp.next;
}
}
}
3. 单链表的反转
方法一:
- 利用头插法,将原链表的数据添加到新链表中(双指针)
- 然后让原链表的表头指向新链表的数据
方法二:
- 也可以直接用头插法,将数据添加到原链表的表头上。
缺点:会改变原链表的结构
3.2 代码实现 4. 从尾到头打印单链表
演示栈的基本使用:
方法一:
- 将单链表反转后,再打印输出。由于会改变单链表的结构,所以不推荐使用
方法二:
- 利用栈的先进后出特性。将节点存放到栈中后,再取出。
package com.SingleLinkedListPrac04;
import java.util.Stack;
public class SingleLinkedListDemo {
public static void main(String[] args) {
HeroNode heroNode1 = new HeroNode(1, "宋江", "及时雨");
HeroNode heroNode2 = new HeroNode(2, "晁盖", "玉麒麟");
HeroNode heroNode3 = new HeroNode(3, "吴用", "智多星");
SingleLinkedList singleLinkedList = new SingleLinkedList();
singleLinkedList.add(heroNode1);
singleLinkedList.add(heroNode2);
singleLinkedList.add(heroNode3);
singleLinkedList.list();
//有效节点的个数测试
System.out.println("有效节点的个数为:" + getLength(singleLinkedList.getHead()));
//单链表中的倒数第k个节点测试
HeroNode res = findLastIndexNode(singleLinkedList.getHead(), 1);
System.out.println("res = " + res);
//从尾到头打印列表测试
System.out.println("从尾到头打印列表:");
reversePrint(singleLinkedList.getHead());
}
// 9. 单链表中有效节点的个数
public static int getLength(HeroNode head) {
if (head.next == null) {
return 0;
}
int length = 0;
HeroNode temp = head;
while (temp.next != null) {
length++;
temp = temp.next;
}
return length;
}
// 10. 查找单链表中的倒数第k个节点
public static HeroNode findLastIndexNode(HeroNode head, int k) {
if (head.next == null) {
return null;
}
int length = getLength(head);
if (k <= 0 || k > length) {
return null;
}
HeroNode cur = head.next;
for (int i = 0; i < (length - k); i++) {
cur = cur.next;
}
return cur;
}
// 11. 单链表的反转
public static void reverseList(HeroNode head) {
if (head.next == null || head.next.next == null) {
return;
}
// 反转后的新链表的表头
HeroNode reverseHead = new HeroNode(0, "", "");
HeroNode cur = head.next;
HeroNode next = null; //用来指向当前节点的下一个节点
while (cur != null) {
next = cur.next;
cur.next = reverseHead.next;
reverseHead.next = cur;
cur = next;
}
head.next = reverseHead.next;
}
// 12. 从尾到头打印单链表
public static void reversePrint(HeroNode head) {
if (head.next == null) {
return; // 空列表,不能打印
}
Stack stack = new Stack<>();
HeroNode cur = head.next;
while (cur != null) {
stack.push(cur);
cur = cur.next;
}
while (stack.size() > 0) {
System.out.println(stack.pop());
}
}
// 13. 合并两个有序的单链表,合并后依然有序
}
class HeroNode {
// 1. 创建一个节点类,用来存放节点
// 2. 添加节点类的属性
public int no;
public String name;
public String nickname;
public HeroNode next;
// 3. 添加构造方法,初始化属性
public HeroNode(int no, String name, String nickname) {
this.no = no;
this.name = name;
this.nickname = nickname;
}
// 4. toString方法,显示节点信息
@Override
public String toString() {
return "HeroNode{" +
"no=" + no +
", name='" + name + ''' +
", nickname='" + nickname + ''' +
'}';
}
}
class SingleLinkedList {
// 5. 创建一个单链表的类,用来添加对单链表的操作
// 6. 添加节点
private HeroNode head = new HeroNode(0, "", "");
public HeroNode getHead() {
return this.head;
}
public void add(HeroNode heroNode) {
HeroNode temp = head;
while (temp.next != null) {
temp = temp.next;
}
temp.next = heroNode;
}
// 7. 有序添加节点
public void addByOrder(HeroNode heroNode) {
HeroNode temp = head;
while (temp.next != null) {
if (temp.next.no < heroNode.no) {
temp = temp.next;
}else if (temp.next.no > heroNode.no) {
heroNode.next = temp.next;
break;
}else {
System.out.printf("编号为 %d 的节点已经存在,添加失败~~~n", heroNode.no);
return;
}
temp.next = heroNode;
}
}
// 8. 显示单链表的节点信息
public void list() {
if (head.next == null) {
System.out.println("该单链表为空,没有可以显示的节点信息~~~");
return;
}
HeroNode temp = head;
while (temp.next != null) {
System.out.println(temp.next);
temp = temp.next;
}
}
}
5. 合并两个有序的单链表,合并后依然有序
5.1 思路
- 如果有一个单链表为空,则直接合并另一个单链表即可
- 当两个单链表都不为空时,比较两个单链表的no,通过三个临时变量(cur、cur1、cur2)来控制链表数据的移动。cur控制合并后的链表、cur1控制第一个链表、cur2控制第二个链表的数据移动
- 直到其中一个单链表为空时,把另一个链表直接添加进来,最后返回单链表对象即可
package com.SingleLinkedListPrac04;
import java.util.Stack;
public class SingleLinkedListDemo {
public static void main(String[] args) {
HeroNode heroNode1 = new HeroNode(1, "宋江", "及时雨");
HeroNode heroNode5 = new HeroNode(5, "公孙胜", "入云龙");
HeroNode heroNode6 = new HeroNode(6, "关胜", "大刀");
SingleLinkedList singleLinkedList = new SingleLinkedList();
singleLinkedList.add(heroNode1);
singleLinkedList.add(heroNode5);
singleLinkedList.add(heroNode6);
System.out.println("第一个有序列表:");
singleLinkedList.list();
//合并两个有序的单链表,合并后依然有序测试
HeroNode heroNode2 = new HeroNode(2, "晁盖", "玉麒麟");
HeroNode heroNode3 = new HeroNode(3, "吴用", "智多星");
HeroNode heroNode4 = new HeroNode(4, "林冲", "豹子头");
SingleLinkedList singleLinkedList2 = new SingleLinkedList();
singleLinkedList2.add(heroNode2);
singleLinkedList2.add(heroNode3);
singleLinkedList2.add(heroNode4);
System.out.println("第二个有序列表:");
singleLinkedList2.list();
System.out.println("合并后的有序列表:");
SingleLinkedList mergedSingleLinkedList = mergeOrderedLink(singleLinkedList.getHead(), singleLinkedList2.getHead());
mergedSingleLinkedList.list();
}
// 9. 单链表中有效节点的个数
public static int getLength(HeroNode head) {
if (head.next == null) {
return 0;
}
int length = 0;
HeroNode temp = head;
while (temp.next != null) {
length++;
temp = temp.next;
}
return length;
}
// 10. 查找单链表中的倒数第k个节点
public static HeroNode findLastIndexNode(HeroNode head, int k) {
if (head.next == null) {
return null;
}
int length = getLength(head);
if (k <= 0 || k > length) {
return null;
}
HeroNode cur = head.next;
for (int i = 0; i < (length - k); i++) {
cur = cur.next;
}
return cur;
}
// 11. 单链表的反转
public static void reverseList(HeroNode head) {
if (head.next == null || head.next.next == null) {
return;
}
// 反转后的新链表的表头
HeroNode reverseHead = new HeroNode(0, "", "");
HeroNode cur = head.next;
HeroNode next = null; //用来指向当前节点的下一个节点
while (cur != null) {
next = cur.next;
cur.next = reverseHead.next;
reverseHead.next = cur;
cur = next;
}
head.next = reverseHead.next;
}
// 12. 从尾到头打印单链表
public static void reversePrint(HeroNode head) {
if (head.next == null) {
return; // 空列表,不能打印
}
Stack stack = new Stack<>();
HeroNode cur = head.next;
while (cur != null) {
stack.push(cur);
cur = cur.next;
}
while (stack.size() > 0) {
System.out.println(stack.pop());
}
}
// 13. 合并两个有序的单链表,合并后依然有序
public static SingleLinkedList mergeOrderedLink(HeroNode head1, HeroNode head2) {
HeroNode mergedHeroNode = new HeroNode(0, "", "");
SingleLinkedList mergedSingleLinkedList = new SingleLinkedList();
if (head1.next == null) {
mergedSingleLinkedList.add(head2.next);
return mergedSingleLinkedList;
}
if (head2.next == null) {
mergedSingleLinkedList.add(head1.next);
return mergedSingleLinkedList;
}
HeroNode cur = mergedHeroNode;
HeroNode cur1 = head1.next;
HeroNode cur2 = head2.next;
while (cur1 != null && cur2 != null) {
if (cur1.no < cur2.no) {
cur.next = cur1;
cur = cur.next;
cur1 = cur1.next;
}else if (cur1.no > cur2.no) {
cur.next = cur2;
cur = cur.next;
cur2 = cur2.next;
}else {
mergedSingleLinkedList.add(cur1);
cur.next = cur1.next;
cur = cur.next;
cur1 = cur1.next;
cur2 = cur2.next;
}
}
if (cur1 == null) {
cur.next = cur2;
}else if (cur2 == null) {
cur.next = cur1;
}
mergedSingleLinkedList.add(mergedHeroNode.next);
return mergedSingleLinkedList;
}
}
class HeroNode {
// 1. 创建一个节点类,用来存放节点
// 2. 添加节点类的属性
public int no;
public String name;
public String nickname;
public HeroNode next;
// 3. 添加构造方法,初始化属性
public HeroNode(int no, String name, String nickname) {
this.no = no;
this.name = name;
this.nickname = nickname;
}
// 4. toString方法,显示节点信息
@Override
public String toString() {
return "HeroNode{" +
"no=" + no +
", name='" + name + ''' +
", nickname='" + nickname + ''' +
'}';
}
}
class SingleLinkedList {
// 5. 创建一个单链表的类,用来添加对单链表的操作
// 6. 添加节点
private HeroNode head = new HeroNode(0, "", "");
public HeroNode getHead() {
return this.head;
}
public void add(HeroNode heroNode) {
HeroNode temp = head;
while (temp.next != null) {
temp = temp.next;
}
temp.next = heroNode;
}
// 7. 有序添加节点
public void addByOrder(HeroNode heroNode) {
HeroNode temp = head;
while (temp.next != null) {
if (temp.next.no < heroNode.no) {
temp = temp.next;
}else if (temp.next.no > heroNode.no) {
heroNode.next = temp.next;
break;
}else {
System.out.printf("编号为 %d 的节点已经存在,添加失败~~~n", heroNode.no);
return;
}
}
temp.next = heroNode;
}
// 8. 显示单链表的节点信息
public void list() {
if (head.next == null) {
System.out.println("该单链表为空,没有可以显示的节点信息~~~");
return;
}
HeroNode temp = head;
while (temp.next != null) {
System.out.println(temp.next.toString());
temp = temp.next;
}
}
}



