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;
}
}
public
void
print(Node node) {
if
(node == null )
{
return ;
}
current
= node;
while
(current != null )
{
System.out.println(current.data);
current
= current.next;
}
}
class
Node {
int
data;
Node
next;
public
Node( int
data) {
this .data
= data;
}
}
public
static
void
main(String[] args) {
LinkList
list = new
LinkList();
for
( int
i = 0 ;
i < 10 ;
i++) {
list.add(i);
}
list.print(list.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) {
if
(head == null )
{
return
- 1 ;
}
current
= head;
while
(current != null )
{
size++;
current
= current.next;
}
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;
for
( int
i = 0 ;
i < index; i++) {
second
= second.next;
}
while
(second != null )
{
first
= first.next;
second
= second.next;
}
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;
for
( int
i = 0 ;
i < k - 1 ;
i++) {
System.out.println( "i的值是"
+ i);
second
= second.next;
if
(second == null )
{
return
null ;
}
}
while
(second.next != null )
{
first
= first.next;
second
= second.next;
}
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;
while
(second != null
&& second.next != null )
{
first
= first.next;
second
= second.next.next;
}
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;
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.next;
head1
= head1.next;
}
else
{
current.next
= head2;
current
= current.next;
head2
= head2.next;
}
}
if
(head1 != null )
{
current.next
= head1;
}
if
(head2 != null )
{
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();
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);
list3.print(list3.head);
}
|