1、单链表的创建和遍历:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
|
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,用来表示当前节点的索引(03行)。
这里面的遍历链表的方法(20行)中,参数node表示从node节点开始遍历,不一定要从head节点遍历。
2、求单链表中节点的个数:
注意检查链表是否为空。时间复杂度为O(n)。这个比较简单。
核心代码:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
//方法:获取单链表的长度
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),大概思路如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
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)
代码实现:(初版)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
|
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大于链表中节点个数时,就会报空指针异常,所以这里需要做一下判断。
核心代码如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
|
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)。
代码实现:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
//方法:查找链表的中间结点
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))
代码实现:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
|
//两个参数代表的是两个链表的头结点
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;
}
|
代码测试:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
public
static
void
main(String[] args) {
LinkList list1 =
new
LinkList();
LinkList list2 =
new
LinkList();
//向LinkList中添加数据
for
(
int
i =
0
; i <
4
; i++) {
list1.add(i);
}
for
(
int
i =
3
; i <
8
; i++) {
list2.add(i);
}
LinkList list3 =
new
LinkList();
list3.head = list3.mergeLinkList(list1.head, list2.head);
//将list1和list2合并,存放到list3中
list3.print(list3.head);
// 从head节点开始遍历输出
}
|