
这份笔记整理了整整一个星期,每一行代码都是自己默写完成,并测试运行失败,同时也解读了一下《剑指offer》这本书中和链表有关的讲解,希望对考试和面试有所帮助。
本文包含链表的下面内容:
1、单链表的构建和数组
2、求单链表中节点的个数
3、查找单链表中的倒数第k个结点(剑指offer,题15)
4、查找单链表中的后面节点
5、合并两个有序的单链表,合并以后的链表一直有序【出现频率高】(剑指offer,题17)
6、单链表的反转【出现频率最高】(剑指offer,题16)
7、从尾到头打印单链表(剑指offer,题5)
8、判断单链表是否有环
9、取出有环链表中,环的长度
10、单链表中,取出环的起始点(剑指offer,题56)。本题需借助前面的第8题和第9题。
11、判断两个单链表相交的第一个交点(剑指offer,题37)
1、单链表的构建和遍历:
public class LinkList {
public Node head;
public Node current;
//方法:向链表中添加数据
public void add(int data) {
//判断链表为空的时候
if (head == null) {//如果头结点为空,说明这个链表还没有创建,那就把新的结点赋给头结点
head = new Node(data);
current = head;
} else {
//创建新的结点,放在当前节点的后面(把新的结点合链表进行关联)
current.next = new Node(data);
//把链表的当前索引向后移动一位
current = current.next; //此步操作完成之后,current结点指向新添加的那个结点
}
}
//方法:遍历链表(打印输出链表。方法的参数表示从节点node开始进行遍历
public void print(Node node) {
if (node == null) {
return;
}
current = node;
while (current != null) {
System.out.println(current.data);
current = current.next;
}
}
class Node {
//注:此处的两个成员变量权限不能为private,因为private的权限是仅对本类访问。
int data; //数据域
Node next;//指针域
public Node(int data) {
this.data = data;
}
}
public static void main(String[] args) {
LinkList list = new LinkList();
//向LinkList中添加数据
for (int i = 0; i < 10; i++) {
list.add(i);
}
list.print(list.head);// 从head节点开始遍历输出
}
}
上方代码中,这上面的Node节点采用的是外部类来表示(33行)。使用内部类的最大好处是可以和内部类进行私有操作的相互访问。
注:内部类访问的特征是:内部类可以直接访问内部类的成员,包括私有;外部类要访问内部类的成员,必须先创建对象。
为了便于添加和遍历的操作,在LinkList类中添加一个成员函数current链表结构 java代码,用来表示当前节点的索引(03行)。
这上面的递归链表的方式(20行)中,参数node表示从node节点开始递归,不一定要从head节点遍历。
2、求单链表中节点的个数:
注意检查链表是否为空。时间复杂度为O(n)。这个非常简单。
核心代码:
//方法:获取单链表的长度
public int getLength(Node head) {
if (head == null) {
return 0;
}
int length = 0;
Node current = head;
while (current != null) {
length++;
current = current.next;
}
return length;
}
3、查找单链表中的倒数第k个结点:
3.1 普通思路:
先将整个数组从头到尾遍历一次,计算出数组的尺寸size,得到链表的尺寸时候,就好办了,直接输出第(size-k)个节点就可以了(注意链表为空,k为0,k为1,k大于数组中结点个数时的状况
)。时间复杂度为O(n),大概思路如下:
public int findLastNode(int index) { //index代表的是倒数第index的那个结点
//第一次遍历,得到链表的长度size
if (head == null) {
return -1;
}
current = head;
while (current != null) {
size++;
current = current.next;
}
//第二次遍历,输出倒数第index个结点的数据
current = head;
for (int i = 0; i < size - index; i++) {
current = current.next;
}
return current.data;
}

如果面试官不允许你递归链表的长度,该如何做呢?接下来就是。
3.2 改进策略:(这种模式在其它题目中还有应用)
这里应该声明两个指针:即两个结点型的变量first和second,首先让first和second都指向第一个结点,然后让second结点往下挪k-1个位置,此时first和second就间隔了k-1个位置,然后整体向后移动这两个节点,直到second节点走到最后一个结点的时侯,此时first节点所指向的位置就是倒数第k个节点的位置。时间复杂度为O(n)
代码实现:(初版)
public Node findLastNode(Node head, int index) {
if (node == null) {
return null;
}
Node first = head;
Node second = head;
//让second结点往后挪index个位置
for (int i = 0; i < index; i++) {
second = second.next;
}
//让first和second结点整体向后移动,直到second结点为Null
while (second != null) {
first = first.next;
second = second.next;
}
//当second结点为空的时候,此时first指向的结点就是我们要找的结点
return first;
}
代码实现:(最终版)(考虑k小于数组中节点个数时的状况时,抛出异常)
上面的代码中,看似即将实现了功能,其实还不够健壮:
要切记k等于0的情况;
如果k大于数组中结点个数时,就会报空指针异常,所以此处应该做一下判断。
核心代码如下:
public Node findLastNode(Node head, int k) {
if (k == 0 || head == null) {
return null;
}
Node first = head;
Node second = head;
//让second结点往后挪k-1个位置
for (int i = 0; i < k - 1; i++) {
System.out.println("i的值是" + i);
second = second.next;
if (second == null) { //说明k的值已经大于链表的长度了
//throw new NullPointerException("链表的长度小于" + k); //我们自己抛出异常,给用户以提示
return null;
}
}
//让first和second结点整体向后移动,直到second走到最后一个结点
while (second.next != null) {
first = first.next;
second = second.next;
}
//当second结点走到最后一个节点的时候,此时first指向的结点就是我们要找的结点
return first;
}
4、查找单链表中的后面节点:
同样,面试官不允许你算出链表的长度,该如何做呢?
思路:
和前面的第2节一样,也是修改两个指针first和second,只不过这里是,两个指针同时向前走,second指针每次走两步,first指针每次走一步,直到second指针走到最后一个结点时,此时first指针所指的节点就是中间结点。注意数组为空,链表节点个数为1和2的状况。时间复杂度为O(n)。
代码实现:
//方法:查找链表的中间结点
public Node findMidNode(Node head) {
if (head == null) {
return null;
}
Node first = head;
Node second = head;
//每次移动时,让second结点移动两位,first结点移动一位
while (second != null && second.next != null) {
first = first.next;
second = second.next.next;
}
//直到second结点移动到null时,此时first指针指向的位置就是中间结点的位置
return first;
}
上方代码中,当n为偶数时,得到的后面结点是第n/2 + 1个结点。比如数组有6个节点时,得到的是第4个节点。
5、合并两个有序的单链表,合并以后的链表一直有序:
这道题就会被各公司考察。
例如:
链表1:
1->2->3->4
链表2:
2->3->4->5
合并后:
1->2->2->3->3->4->4->5
解题思路:

挨着比较链表1和链表2。
这个类似于归并排序。尤其要切记两个链表都为空、和其中一个为空的状况。只应该O (1) 的空间。时间复杂度为O (max(len1,len2))
代码实现:
//两个参数代表的是两个链表的头结点
public Node mergeLinkList(Node head1, Node head2) {
if (head1 == null && head2 == null) { //如果两个链表都为空
return null;
}
if (head1 == null) {
return head2;
}
if (head2 == null) {
return head1;
}
Node head; //新链表的头结点
Node current; //current结点指向新链表
// 一开始,我们让current结点指向head1和head2中较小的数据,得到head结点
if (head1.data < head2.data) {
head = head1;
current = head1;
head1 = head1.next;
} else {
head = head2;
current = head2;
head2 = head2.next;
}
while (head1 != null && head2 != null) {
if (head1.data < head2.data) {
current.next = head1; //新链表中,current指针的下一个结点对应较小的那个数据
current = current.next; //current指针下移
head1 = head1.next;
} else {
current.next = head2;
current = current.next;
head2 = head2.next;
}
}
//合并剩余的元素
if (head1 != null) { //说明链表2遍历完了,是空的
current.next = head1;
}
if (head2 != null) { //说明链表1遍历完了,是空的
current.next = head2;
}
return head;
}
本文来自电脑杂谈,转载请注明本文网址:
http://www.pc-fly.com/a/jisuanjixue/article-122053-1.html
入台湾之日