删除链表中的节点

写一个函数,使其可以删除某个链表中给定的(非末尾)节点,你将只被给定要求被删除的节点。

因为函数只给定了一个参数,表面该节点就是要删除的结点。然后直接用下一个结点覆当前结点。

开始没想到,因为一般都是先找前一个结点,以及下一个结点,这道题是直接用下一个结点覆盖当前结点。

/**
 * 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) {    
    node->val = node->next->val;
    node->next = node->next->next;        
    }
};
The Safest Way to Get what you Want is to Try and Deserve What you Want.
原文地址:https://www.cnblogs.com/Shinered/p/11419963.html