1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
| void deleteAtBeginning(Node* &pHead)
{
Node* cur = pHead;
if (pHead) pHead = pHead->pNext;
delete cur;
}
void deleteANode(Node*& pHead, int k)
{
if(pHead != nullptr && pHead->data == k) {
deleteAt(pHead);
return;
}
Node* tmp = nullptr;
Node* cur = pHead;
while(cur != nullptr && cur->data != k) {
tmp = cur;
cur = cur->pNext;
}
if (cur == nullptr) return;
tmp->pNext = cur->pNext;
delete cur;
}
void deleteAllNode(Node*& pHead, int k)
{
while (pHead != nullptr && pHead->data == k) deleteAt(pHead);
if (pHead == nullptr) return;
for (Node *cur = pHead; cur->pNext != nullptr; ) {
if(cur->pNext->data == k)
deleteAt(cur->pNext);
else
cur = cur->pNext;
}
}
|