Leetcode: delete the node in the linked list

Please write a function that can delete the non-tailed nodes in the linked list, you will only be given the node you want to delete, for example: there is now a linked list - head=[ 4,5,1,9] Delete 5 this node

原
4->5->1->9->NULL
After deletion
4->1->9->NULL

Description: 1. The linked list contains at least two nodes 2. All nodes in the linked list are unique 3. The given node is a non-tail node and must be a valid node in the linked list 4. Don't return any results from your function 思维:Delete a non-tail node and don't give you the head node of the linked list. We only know the node to be deleted and the node after this list, so we You can use the replacement method, first replace the node with the next node to delete the node, and then delete the next node to meet our effect, as shown below: 在这里插入图片描述 在这里插入图片描述 在这里插入图片描述 在这里插入图片描述

Code is as follows:

class Solution:{
public:
	void deleteNode(ListNode*node)
	{
		ListNode*next=node->next;
		node->val=next->val;
		node->next=next->next;
		free(next);
	}
};