本周做一个单链表操作的题目。边写代码边测试,弄了近一个小时,中间有被中断,虽然一次就是acc了,但速度还是不够快。而且运行时间也不够快,分析了一下,可以使用头尾指针和长度运行时间会更快。比如插表尾法可以从O(n)降为O(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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
public class MyLinkedList {

private class ListNode {

public int val;

public ListNode next;

public ListNode(int x) {
val = x;
next = null;
}
}

private ListNode myLinkedList;

/** Initialize your data structure here. */
public MyLinkedList() {
myLinkedList = null;
}

/** Get the value of the index-th node in the linked list. If the index is invalid, return -1. */
public int get(int index) {
if (null == myLinkedList) {
return -1;
}
int i = 0;
ListNode currNode = myLinkedList;
while (currNode != null) {
if (i == index) {
return currNode.val;
}
currNode = currNode.next;
++i;
}
return -1;
}

/** Add a node of value val before the first element of the linked list.
* After the insertion, the new node will be the first node of the linked list.
**/
public void addAtHead(int val) {
ListNode newNode = new ListNode(val);
ListNode oldHeadNode = myLinkedList;
newNode.next = oldHeadNode;
myLinkedList = newNode;
}

/** Append a node of value val to the last element of the linked list. */
public void addAtTail(int val) {
ListNode newNode = new ListNode(val);
// first node
if (null == myLinkedList) {
myLinkedList = newNode;
}
ListNode preNode = null;
ListNode currNode = myLinkedList;
while (currNode != null) {
preNode = currNode;
currNode = currNode.next;
}
preNode.next = newNode;
}

/** Add a node of value val before the index-th node in the linked list.
* If index equals to the length of linked list, the node will be appended to the end of linked list.
* If index is greater than the length, the node will not be inserted. */
public void addAtIndex(int index, int val) {
if (index < 0) {
return;
}
if (0 == index) {
this.addAtHead(val);
return;
}
// linked list is empty, but index is greater than the length
if (null == myLinkedList) {
return;
}
ListNode preNode = null;
ListNode currNode = myLinkedList;
int i = 0;
while (currNode != null) {
if (i == index) {
break;
}
preNode = currNode;
currNode = currNode.next;
++i;
}

// when the index in linked list, then i == index, other is the length
if (index == i) {
ListNode newNode = new ListNode(val);
newNode.next = preNode.next;
preNode.next = newNode;
}
return;
}

/** Delete the index-th node in the linked list, if the index is valid. */
public void deleteAtIndex(int index) {
if (index < 0) {
return;
}
if (null == myLinkedList) {
return;
}
ListNode preNode = null;
ListNode currNode = myLinkedList;

while (currNode != null) {
if (0 == index) {
if (preNode != null) { // find index node
preNode.next = currNode.next;
} else { // first node
myLinkedList = currNode.next;
}
currNode = null;
break;
}
preNode = currNode;
currNode = currNode.next;
--index;
}
}
}