[LeetCode][JavaScript]Delete Node in a Linked List

Delete Node in a Linked List

Write a function to delete a node (except the tail) in a singly linked list, given only access to that node.

Supposed the linked list is 1 -> 2 -> 3 -> 4 and you are given the third node with value 3, the linked list should become 1 -> 2 -> 4 after calling your function.

https://leetcode.com/problems/delete-node-in-a-linked-list/


删除单链表中的一个给定的节点,但是不给这个链表的表头。

解法很简单,把后一个节点的值覆盖当前节点的值,一直做到最后,删除最后一个节点。

 1 /**
 2  * @param {ListNode} node
 3  * @return {void} Do not return anything, modify node in-place instead.
 4  */
 5 var deleteNode = function(node) {
 6     var previous = node;
 7     delNode(node.next);
 8     return;
 9     
10     function delNode(n){
11         previous.val = n.val;
12         if(n.next === null){
13             previous.next = null;
14         }else{
15             previous = previous.next;
16             n = n.next;
17             delNode(n);
18         }
19     }
20 };
原文地址:https://www.cnblogs.com/Liok3187/p/4649175.html