707-设计链表

设计链表的实现

   您可以选择使用单链表或双链表。单链表中的节点应该具有两个属性:val 和 next。val 是当前节点的值,next 是指向下一个节点的指针/引用。如果要使用双向链表,则还需要一个属性 prev 以指示链表中的上一个节点。假设链表中的所有节点都是 0-index 的。

在链表类中实现这些功能:

  • get(index):获取链表中第 index 个节点的值。如果索引无效,则返回-1。
  • addAtHead(val):在链表的第一个元素之前添加一个值为 val 的节点。插入后,新节点将成为链表的第一个节点。
  • addAtTail(val):将值为 val 的节点追加到链表的最后一个元素。
  • addAtIndex(index,val):在链表中的第 index 个节点之前添加值为 val 的节点。如果 index 等于链表的长度,则该节点将附加到链表的末尾。如果 index 大于链表长度,则不会插入节点。
  • deleteAtIndex(index):如果索引 index 有效,则删除链表中的第 index 个节点。

示例:

1
2
3
4
5
6
7
MyLinkedList linkedList = new MyLinkedList();
linkedList.addAtHead(1);
linkedList.addAtTail(3);
linkedList.addAtIndex(1,2); //链表变为1-> 2-> 3
linkedList.get(1); //返回2
linkedList.deleteAtIndex(1); //现在链表是1-> 3
linkedList.get(1); //返回3

提示:

  • 所有值都在 [1, 1000] 之内。
  • 操作次数将在 [1, 1000] 之内。
  • 请不要使用内置的 LinkedList 库。

解答:

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
class MyLinkedList {
class Cell {
int val = 0;
Cell next = null;
}

Cell head = null;
Cell tail = null;
int cnt = 0;

/**
* 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 (cnt == 0)
return -1;
if (index == 0) {
return head.val;
} else {
Cell tmp = GetPreNode(index);
if (tmp == null || tmp.next == null)
return -1;
return tmp.next.val;
}

}

/**
* 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) {
this.addAtIndex(0, val);
}

/**
* Append a node of value val to the last element of
the linked list.
*/
public void addAtTail(int val) {
this.addAtIndex(cnt, val);
}

/**
* 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 > cnt)
return;
if (index < 0)
index = 0;
Cell node = new Cell();
node.val = val;
if (cnt == 0) {
head = node;
tail = node;
} else if (index == 0) {
node.next = head;
head = node;
} else {
Cell pre = GetPreNode(index);
if (pre == null)
return;
node.next = pre.next;
pre.next = node;

}
cnt++;
}

/**
* Delete the index-th node in the linked list,
if the index is valid.
*/
public void deleteAtIndex(int index) {
if (index < 0 || index >= cnt)
return;
if (index == 0) {
head = head.next;
} else {
Cell pre = GetPreNode(index);
if (pre == null)
return;
if (pre.next != null) {
pre.next = pre.next.next;
} else {
pre.next = null;
}

}
cnt--;
}

public Cell GetPreNode(int index) {
if (index < 0 || index > cnt)
return null;
Cell pre = head;
int tmpCnt = 1;
while (tmpCnt < index) {
if (pre.next == null)
return null;
pre = pre.next;
tmpCnt++;
}
return pre;
}

}

主要的坑

  • 要记录当前节点数,每次增加删除节点要加减
  • addAtIndex调用时,如果index参数小于0,要改变其值为0
  • 添加删除节点时候,如果时链条中的,记得连接两头