237. 删除链表中的节点

请编写一个函数,使其可以删除某个链表中给定的(非末尾的)节点,您将只被给予要求被删除的节点。

比如:假设该链表为 1 -> 2 -> 3 -> 4 ,给定您的为该链表中值为 3 的第三个节点,那么在调用了您的函数之后,该链表则应变成 1 -> 2 -> 4 。
后移指针
img

img

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode(int x) : val(x), next(NULL) {}
 * };
 */
class Solution {
public:
    void deleteNode(ListNode* node) {
        node->val = node->next->val;
        node->next = node->next->next;
    }
};

猜你喜欢

转载自blog.csdn.net/hy971216/article/details/80556941