mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-12 05:20:59 +08:00
添加 19删除链表的倒数第N个节点 Kotlin实现
This commit is contained in:
@ -184,6 +184,25 @@ var removeNthFromEnd = function(head, n) {
|
|||||||
return ret.next;
|
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)
|
* 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw)
|
||||||
|
Reference in New Issue
Block a user