This commit is contained in:
krahets
2024-04-07 03:05:15 +08:00
parent aea68142f8
commit d8caf02e9e
21 changed files with 118 additions and 101 deletions
@@ -597,7 +597,7 @@ comments: true
=== "Kotlin"
```kotlin title="linked_list.kt"
/* 在链表的节点 n0 之后插入节点p */
/* 在链表的节点 n0 之后插入节点 P */
fun insert(n0: ListNode?, p: ListNode?) {
val n1 = n0?.next
p?.next = n1
@@ -811,9 +811,11 @@ comments: true
```kotlin title="linked_list.kt"
/* 删除链表的节点 n0 之后的首个节点 */
fun remove(n0: ListNode?) {
val p = n0?.next
if (n0?.next == null)
return
val p = n0.next
val n1 = p?.next
n0?.next = n1
n0.next = n1
}
```
@@ -1018,7 +1020,9 @@ comments: true
fun access(head: ListNode?, index: Int): ListNode? {
var h = head
for (i in 0..<index) {
h = h?.next
if (h == null)
return null
h = h.next
}
return h
}
@@ -1249,7 +1253,8 @@ comments: true
var index = 0
var h = head
while (h != null) {
if (h.value == target) return index
if (h.value == target)
return index
h = h.next
index++
}