https://www.nowcoder.com/practice/6a668a3960e24d3ea04bba89109c6451?tpId=8&tqId=11003&tPage=1&rp=1&ru=/ta/cracking-the-coding-interview&qru=/ta/cracking-the-coding-interview/question-ranking

题目描述

实现一个算法,删除单向链表中间的某个结点,假定你只能访问该结点。

给定带删除的节点,请执行删除操作,若该节点为尾节点,返回false,否则返回true

感觉自己要把大力气花费在指针相关的面试题上啊

O(1)时间复杂度:

把下一个结点赋给欲删除结点,把下一个节点的next赋给欲删除结点的next

/*
struct ListNode {
    int val;
    struct ListNode *next;
    ListNode(int x) : val(x), next(NULL) {}
};*/
class Remove {
public:
    bool removeNode(ListNode* pNode) {
        // write code here
        if(pNode->next!=NULL)
        {
            pNode->val=pNode->next->val;
            pNode->next=pNode->next->next;
            return true;
        }
        else
        {
            pNode=NULL;
            return false;
        }
    }
};