diff --git a/problems/0707.设计链表.md b/problems/0707.设计链表.md index f7b03c8b..5e748a24 100644 --- a/problems/0707.设计链表.md +++ b/problems/0707.设计链表.md @@ -917,16 +917,17 @@ class MyLinkedList { } fun addAtIndex(index: Int, `val`: Int) { - if (index == 0) return addAtHead(`val`) - if (index == size) return addAtTail(`val`) if (index > size) return - var cur = this.next - for (i in 0 until index - 1) { + val pre = ListNode(0) + pre.next = this.next + var cur:ListNode? = pre + for (i in 0 until index) { cur = cur?.next } val temp = cur?.next cur?.next = ListNode(`val`) cur?.next?.next = temp + this.next = pre.next size++ }