Skip to content

Commit

Permalink
添加 19删除链表的倒数第N个节点 Kotlin实现
Browse files Browse the repository at this point in the history
  • Loading branch information
GuoDuanLZ committed Aug 7, 2021
1 parent 9dd846e commit 0f4c80a
Showing 1 changed file with 19 additions and 0 deletions.
19 changes: 19 additions & 0 deletions problems/0019.删除链表的倒数第N个节点.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 0f4c80a

Please sign in to comment.