Appearance
019删除倒数第n个节点
code
javascript
/**
* Definition for singly-linked list.
* function ListNode(val, next) {
* this.val = (val===undefined ? 0 : val)
* this.next = (next===undefined ? null : next)
* }
*/
/**
* @param {ListNode} head
* @param {number} n
* @return {ListNode}
*/
var removeNthFromEnd = function(head, n) {
if(head.next === null && n === 1){
return null;
}
let ind_slow = head;
let ind_fast = head;
for(let i = 0; i < n; i++){
ind_fast = ind_fast.next;
}
if(ind_fast === null) {
return head;
}
while(ind_fast.next !== null){
ind_slow = ind_slow.next;
ind_fast = ind_fast.next;
}
ind_slow.next = ind_slow.next.next;
return head;
};总结
- 貌似是一眼快慢指针