leetcode 237. Delete Node in a Linked List
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.
AC:
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* struct ListNode *next;
* };
*/
void deleteNode(struct ListNode* node) {
struct ListNode* p=node->next;
node->val=p->val;
node->next=p->next;
free(p);
}
tips:删除链表中的node结点,给的参数就是要删除的结点,因为不知道该结点的头节点,所以无法删除该结点,所以就需要将该结点的后一个结点的值覆盖本届点的值,然后删除后一个结点即可。
本文介绍了一种特殊的链表节点删除方法,即仅通过给定待删除节点本身来完成删除操作,而不需要知道其前驱节点。这种方法适用于除尾节点外的所有节点,并提供了详细的实现代码。
451

被折叠的 条评论
为什么被折叠?



