leetcode No237. Delete Node in a Linked List

    xiaoxiao2021-03-25  107

    Question

    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. 删除节点如图

    Algorithm

    如图

    1、先把要删除节点A的下一个节点B的值赋给A 2、再把A的next指向B的next

    Accepted Code:

    class Solution { public: void deleteNode(ListNode* node) { *node=*node->next; } };

    也可以把B结点的地址赋给A结点

    class Solution { public: void deleteNode(ListNode* node) { *node=*node->next; } };
    转载请注明原文地址: https://ju.6miu.com/read-12458.html

    最新回复(0)