http://wuchong.me/blog/2014/03/25/interview-link-questions/
主要思想是##「狸猫换太子」,即用下一个节点数据覆盖要删除的节点,然后删除下一个节点。但是如果节点是尾节点时,该方法就行不通了。
/**
* Definition of ListNode
* class ListNode {
* public:
* int val;
* ListNode *next;
* ListNode(int val) {
* this->val = val;
* this->next = NULL;
* }
* }
*/
class Solution {
public:
/*
* @param node: the node in the list should be deletedt
* @return: nothing
*/
void deleteNode(ListNode * node) {
// write your code here
if(node == NULL || node->next == NULL){
return;
}
ListNode *pnext;
pnext = node->next;
node->val = pnext->val;
node->next = pnext->next;
delete pnext;
}
};