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.
原题链接:https://leetcode.com/problems/delete-node-in-a-linked-list/
思路
太简单了吧,接口直接把node都给出来了,省掉了查找的时间。
更多链表操作可以看这篇文章《链表操作》
代码(C)
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* struct ListNode *next;
* };
*/
void deleteNode(struct ListNode* node)
{
if(node == NULL || node->next == NULL)
return;
node->val = node->next->val;
node->next = node->next->next;
}