Write a function to delete a node (except the tail) in a singly linked list, given only access to that node.
Supposed the linked list is 1 -> 2 -> 3 -> 4
and you are given the third node with value 3
, the linked list should become 1 -> 2 -> 4
after calling your function
思想:利用覆盖的理念,将下一个节点覆盖当前节点并将当前节点的next链接下一节点的next,同时free原来当前节点的下一个节点
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* struct ListNode *next;
* };
*/
void deleteNode(struct ListNode* node) {
struct ListNode* temp=node->next;
node->val=temp->val;
node->next=temp->next;
free(temp);
}