leetcode No237. Delete Node in a Linked List

来源:互联网 发布:js鼠标移动后单击 编辑:程序博客网 时间:2024/06/06 03:17

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;    }};
0 0
原创粉丝点击