[leetcode] 237. Delete Node in a Linked List

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode(int x) : val(x), next(NULL) {}
 * };
 */
class Solution {
public:
    void deleteNode(ListNode* node) {
       if(!node) return;
       node->val=node->next->val;
       node->next=node->next->next;
    }
};

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

1. 没有告诉链表首指针,没有前指针,直接给了要删的节点指针,不需要找特定元素值的节点。

2. 把后一节点的值复制到当前节点,然后删掉后一节点。

原文地址:https://www.cnblogs.com/yuchenkit/p/5253135.html