C++示例:
/**
* 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 == NULL || node->next == NULL) {
cout << "The node is empty or the node is the rear node." << endl;
return;
}
ListNode* temp = node->next;
node->val = temp->val;
node->next = temp->next;
delete temp;
}
};