[LeetCode] 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.

删除链表中的节点,属于链表的基本操作,由于是一个单链表,所以无法得到待删除节点的前序节点,所以将待删除节点的下一个节点的值赋给待删除节点,将待删除节点的指针指向它的后面第二个节点。

class Solution {
public:
    void deleteNode(ListNode* node) {
        ListNode* tmp = node->next;
        node->val = tmp->val;
        node->next = tmp->next;
    }
};
// 16 ms
原文地址:https://www.cnblogs.com/immjc/p/7191849.html