给定一个单向链表中的某个节点,请删除这个节点,但要求只能访问该节点。若该节点为尾节点,返回false,否则返回true。
/*
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) return false;
pNode->val = pNode->next->val;
pNode->next = pNode->next->next;
return true;
}
};
时间复杂度O(1),空间复杂度O(1)。