mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-10 20:40:39 +08:00
添加19.删除链表的倒数第N个节点javascript版本
This commit is contained in:
@ -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)
|
* 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw)
|
||||||
* B站视频:[代码随想录](https://space.bilibili.com/525438321)
|
* B站视频:[代码随想录](https://space.bilibili.com/525438321)
|
||||||
|
Reference in New Issue
Block a user