Implement an algorithm to delete a node in the middle of a singly linked list, given only access to that node.
这道题跟想象的不一样,以为输入给的节点时head,其实不是,而是你要删除的那个点,所以,方法是,将下一个点的值赋予此点,然后删除下一个点。
/**
* 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) {
if (node == NULL || node->next == NULL) {
node = NULL;
}
node->val = node->next->val;
node->next = node->next->next;
}
};