#include <iostream>
#include <string>
using namespace std;
struct ListNode
{
int val;
ListNode* next;
};
void AddToTail(ListNode* *pHead, int value) //为什么传递的是指针的指针呢?
{
ListNode* pNew = new ListNode();
pNew->val = value;
pNew->next = NULL;
if (*pHead == NULL) {
*pHead = pNew;
}
else {
ListNode* p = *pHead;
while (p->next != NULL) {
p = p->next;
}
p->next = pNew;
}
}
int main() {
ListNode* head = NULL;
AddToTail(&head, 10);
//AddToTail(&head, 56);
if (head != NULL) {
cout << head->val << endl;
}
else {
cout << "head is NULL.." << endl;
}
system("pause");
}
`首先,应该有一下两点认识
1: 内存四区的概念
2:形参传递的只是实参的复制品
为了有比较效果,更方便理解,先来看看如果传递的是指针会出现什么样的情况
#include <iostream>
#include <string>
using namespace std;
struct ListNode
{