mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-09 02:53:31 +08:00
在设计链表的C++代码部分,addAtIndex方法中,针对index < 0 的情况纠错
This commit is contained in:
@ -108,9 +108,12 @@ public:
|
|||||||
// 如果index大于链表的长度,则返回空
|
// 如果index大于链表的长度,则返回空
|
||||||
// 如果index小于0,则置为0,作为链表的新头节点。
|
// 如果index小于0,则置为0,作为链表的新头节点。
|
||||||
void addAtIndex(int index, int val) {
|
void addAtIndex(int index, int val) {
|
||||||
if (index > _size || index < 0) {
|
if (index > _size) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
if (index < 0) {
|
||||||
|
index = 0;
|
||||||
|
}
|
||||||
LinkedNode* newNode = new LinkedNode(val);
|
LinkedNode* newNode = new LinkedNode(val);
|
||||||
LinkedNode* cur = _dummyHead;
|
LinkedNode* cur = _dummyHead;
|
||||||
while(index--) {
|
while(index--) {
|
||||||
|
Reference in New Issue
Block a user