mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-13 06:06:11 +08:00
添加(0019.删除链表的倒数第N个节点.md):增加typescript版本
This commit is contained in:
@ -181,7 +181,27 @@ var removeNthFromEnd = function(head, n) {
|
|||||||
return ret.next;
|
return ret.next;
|
||||||
};
|
};
|
||||||
```
|
```
|
||||||
|
TypeScript:
|
||||||
|
|
||||||
|
```typescript
|
||||||
|
function removeNthFromEnd(head: ListNode | null, n: number): ListNode | null {
|
||||||
|
let newHead: ListNode | null = new ListNode(0, head);
|
||||||
|
let slowNode: ListNode | null = newHead,
|
||||||
|
fastNode: ListNode | null = newHead;
|
||||||
|
for (let i = 0; i < n; i++) {
|
||||||
|
fastNode = fastNode.next;
|
||||||
|
}
|
||||||
|
while (fastNode.next) {
|
||||||
|
fastNode = fastNode.next;
|
||||||
|
slowNode = slowNode.next;
|
||||||
|
}
|
||||||
|
slowNode.next = slowNode.next.next;
|
||||||
|
return newHead.next;
|
||||||
|
};
|
||||||
|
```
|
||||||
|
|
||||||
Kotlin:
|
Kotlin:
|
||||||
|
|
||||||
```Kotlin
|
```Kotlin
|
||||||
fun removeNthFromEnd(head: ListNode?, n: Int): ListNode? {
|
fun removeNthFromEnd(head: ListNode?, n: Int): ListNode? {
|
||||||
val pre = ListNode(0).apply {
|
val pre = ListNode(0).apply {
|
||||||
|
Reference in New Issue
Block a user