思路
(注意:这种题虽然简单,但是一定要注意特殊情况的处理。比如删除的是第一个节点。)
代码
/**
* Definition for singly-linked list.
* function ListNode(val) {
* this.val = val;
* this.next = null;
* }
*/
/**
* @param {ListNode} head
* @param {number} val
* @return {ListNode}
*/
var deleteNode = function(head, val) {
if(head.val == val) return head.next;
var cur = head;
var pre = head;
while(cur.val != val) {
pre = cur;
cur = cur.next;
}
pre.next = cur.next;
return head;
};