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;
}