当前位置: 首页 > 工具软件 > easy-tips > 使用案例 >

Easy-36

宋鸿德
2023-12-01

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结点,给的参数就是要删除的结点,因为不知道该结点的头节点,所以无法删除该结点,所以就需要将该结点的后一个结点的值覆盖本届点的值,然后删除后一个结点即可。

 类似资料: