Easy-36

xiaoxiao2021-02-28  87

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

转载请注明原文地址: https://www.6miu.com/read-96826.html

最新回复(0)