链表-Delete Node in a Linked List-简单

描述

给定一个单链表中的一个等待被删除的节点(非表头或表尾)。请在在O(1)时间复杂度删除该链表节点。

样例 Linked list is 1->2->3->4, and given node 3, delete the node in place 1->2->4

题目链接

程序



/**
 * Definition of ListNode
 * class ListNode {
 * public:
 *     int val;
 *     ListNode *next;
 *     ListNode(int val) {
 *         this->val = val;
 *         this->next = NULL;
 *     }
 * }
 */


class Solution {
public:
    /*
     * @param node: the node in the list should be deletedt
     * @return: nothing
     */
    void deleteNode(ListNode * node) {
        // write your code here
        //因为没法找到上一个节点,所以需要用下一个节点对当前进行覆盖
        node->val = node->next->val;
        node->next = node->next->next;
    }
};

猜你喜欢

转载自blog.csdn.net/qq_18124075/article/details/81074986
今日推荐