diff --git a/codes/go/chapter_array_and_linkedlist/my_list.go b/codes/go/chapter_array_and_linkedlist/my_list.go index cd465d581..8f13630be 100644 --- a/codes/go/chapter_array_and_linkedlist/my_list.go +++ b/codes/go/chapter_array_and_linkedlist/my_list.go @@ -79,16 +79,19 @@ func (l *MyList) insert(num, index int) { } /* 删除元素 */ -func (l *MyList) remove(index int) { +func (l *MyList) remove(index int) int { if index >= l.numsSize { panic("索引越界") } + num := l.nums[index] // 索引 i 之后的元素都向前移动一位 for j := index; j < l.numsSize-1; j++ { l.nums[j] = l.nums[j+1] } // 更新元素数量 l.numsSize-- + // 返回被删除元素 + return num } /* 列表扩容 */ @@ -103,4 +106,4 @@ func (l *MyList) extendCapacity() { func (l *MyList) toArray() []int { // 仅转换有效长度范围内的列表元素 return l.nums[:l.numsSize] -} \ No newline at end of file +} diff --git a/codes/python/chapter_array_and_linkedlist/my_list.py b/codes/python/chapter_array_and_linkedlist/my_list.py index 550e66955..bd76fdcd0 100644 --- a/codes/python/chapter_array_and_linkedlist/my_list.py +++ b/codes/python/chapter_array_and_linkedlist/my_list.py @@ -55,11 +55,14 @@ class MyList: """ 删除元素 """ def remove(self, index): assert index < self.__size, "索引越界" + num = self.nums[index] # 索引 i 之后的元素都向前移动一位 for j in range(index, self.__size - 1): self.__nums[j] = self.__nums[j + 1] # 更新元素数量 self.__size -= 1 + # 返回被删除元素 + return num """ 列表扩容 """ def extend_capacity(self):