1、已知当前节点P2,删除该节点。
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/7866b818a5cd83f2185d560bde31f599.png)
#include<iostream>
using namespace std;
struct ListNode {
char val;
ListNode* next;
ListNode(){}
ListNode(char x) : val(x), next(NULL) {}
};
void print(ListNode* pHead) {
ListNode* p = pHead->next;
while (p != NULL) {
cout << p->val << " ";
p = p->next;
}
}
int main() {
ListNode* pHead = new ListNode;
ListNode* P0 = new ListNode('a');
pHead->next = P0;
ListNode* P1 = new ListNode('b');
P0->next = P1;
ListNode* P2 = new ListNode('c');
P1->next = P2;
ListNode* P3 = new ListNode('d');
P2->next = P3;
ListNode* P4 = new ListNode('e');
P3->next = P4;
ListNode* P5 = new ListNode('f');
P4->next = P5;
cout <<"P2的值:"<< P2 << "\n";
cout << "P2的值:" << P2 << "\n";
cout << "P1->next的值:"<<P1->next << "\n";
ListNode* p = P2->next;
P2->val = p->val;
P2->next = p->next;
delete p;
print(pHead);
return 0;
}
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/59d83aaca0765691d1c8e97bc9a5d749.png)
2、已知节点P1,删除下一节点P2。
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/349645cff4bfc1856ad29cf945065330.png)
#include<iostream>
using namespace std;
struct ListNode {
char val;
ListNode* next;
ListNode(){}
ListNode(char x) : val(x), next(NULL) {}
};
void print(ListNode* pHead) {
ListNode* p = pHead->next;
while (p != NULL) {
cout << p->val << " ";
p = p->next;
}
}
int main() {
ListNode* pHead = new ListNode;
ListNode* P0 = new ListNode('a');
pHead->next = P0;
ListNode* P1 = new ListNode('b');
P0->next = P1;
ListNode* P2 = new ListNode('c');
P1->next = P2;
ListNode* P3 = new ListNode('d');
P2->next = P3;
ListNode* P4 = new ListNode('e');
P3->next = P4;
ListNode* P5 = new ListNode('f');
P4->next = P5;
ListNode* p = P1->next;
P1->next = p->next;
delete p;
print(pHead);
return 0;
}
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/6e43774fc3042e731101ba9e853ab8c1.png)