题解 | #删除链表的倒数第n个节点#
删除链表的倒数第n个节点
http://www.nowcoder.com/practice/f95dcdafbde44b22a6d741baf71653f6
快慢指针 复杂度:O(n);O(1)
 * function ListNode(x){
 *   this.val = x;
 *   this.next = null;
 * }
 */
/**
  * 
  * @param head ListNode类 
  * @param n int整型 
  * @return ListNode类
  */
function removeNthFromEnd( head ,  n ) {
    // write code here
    let fast = head,slow = head
    for(let i = 0;i <= n - 1 ;i++){
        fast = fast.next
    }
    if(!fast){
        return head.next
    }
    while(fast.next){
        fast = fast.next
        slow = slow.next
    }
    slow.next = slow.next.next
    return head
}
module.exports = {
    removeNthFromEnd : removeNthFromEnd
};
海康威视公司福利 1144人发布
