Skip to content

Commit

Permalink
优化 707设计链表 addAtIndex
Browse files Browse the repository at this point in the history
  • Loading branch information
GuoDuanLZ committed Aug 7, 2021
1 parent cb65921 commit 91c6ecd
Showing 1 changed file with 5 additions and 4 deletions.
9 changes: 5 additions & 4 deletions problems/0707.设计链表.md
Original file line number Diff line number Diff line change
Expand Up @@ -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++
}

Expand Down

0 comments on commit 91c6ecd

Please sign in to comment.