在设计链表的C++代码部分,addAtIndex方法中,针对index < 0 的情况纠错

This commit is contained in:
Yuhao Ju
2022-11-21 23:12:33 +08:00
committed by GitHub
parent b7d29cbe04
commit 27f8efa24c

View File

@ -108,9 +108,12 @@ public:
// 如果index大于链表的长度则返回空
// 如果index小于0则置为0作为链表的新头节点。
void addAtIndex(int index, int val) {
if (index > _size || index < 0) {
if (index > _size) {
return;
}
if (index < 0) {
index = 0;
}
LinkedNode* newNode = new LinkedNode(val);
LinkedNode* cur = _dummyHead;
while(index--) {