添加19.删除链表的倒数第N个节点javascript版本

This commit is contained in:
qingyi.liu
2021-05-19 11:08:53 +08:00
parent 736897ce44
commit 7253dc9bc7

View File

@ -135,6 +135,28 @@ func removeNthFromEnd(head *ListNode, n int) *ListNode {
}
```
JavaScript:
```js
/**
* @param {ListNode} head
* @param {number} n
* @return {ListNode}
*/
var removeNthFromEnd = function(head, n) {
let ret = new ListNode(0, head),
slow = fast = ret;
while(n--) fast = fast.next;
if(!fast) return ret.next;
while (fast.next) {
fast = fast.next;
slow = slow.next
};
slow.next = slow.next.next;
return ret.next;
};
```
-----------------------
* 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw)
* B站视频[代码随想录](https://space.bilibili.com/525438321)