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);
}
本文介绍了一种在单链表中删除指定节点(除尾节点外)的方法,仅通过访问该节点实现。通过将下一节点的值复制到当前节点并调整指针链接,最终释放下一节点完成删除操作。
984

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



