添加 19删除链表的倒数第N个节点 Kotlin实现

This commit is contained in:
wz-mibookwindows
2021-08-07 12:11:40 +08:00
parent 9dd846ec55
commit 0f4c80a935

View File

@ -184,6 +184,25 @@ var removeNthFromEnd = function(head, n) {
return ret.next;
};
```
Kotlin:
```Kotlin
fun removeNthFromEnd(head: ListNode?, n: Int): ListNode? {
val pre = ListNode(0).apply {
this.next = head
}
var fastNode: ListNode? = pre
var slowNode: ListNode? = pre
for (i in 0..n) {
fastNode = fastNode?.next
}
while (fastNode != null) {
slowNode = slowNode?.next
fastNode = fastNode.next
}
slowNode?.next = slowNode?.next?.next
return pre.next
}
```
-----------------------
* 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw)