//单链表逆置
//3个指针n0,n1,n2
void ReverseList(Node** ppNode)
{
Node* n0 = NULL;
Node* n1 = *ppNode;
Node* n2 = n1->next;
if (*ppNode == NULL)
{
return;
}
while (n1)
{
n1->next = n0;
n0 = n1;
n1 = n2;
if (n2)
n2 = n2->next;
}
*ppNode = n0;
}
void TestTopic1()
{
Node* list = NULL;
Node* tail;
PushBack(&list, 1);
PushBack(&list, 2);
PushBack(&list, 3);
PushBack(&list, 4);
PrintList(list);
ReverseList(&list);
PrintList(list);
}
输出结果:
1234
4321
//3个指针n0,n1,n2
void ReverseList(Node** ppNode)
{
Node* n0 = NULL;
Node* n1 = *ppNode;
Node* n2 = n1->next;
if (*ppNode == NULL)
{
return;
}
while (n1)
{
n1->next = n0;
n0 = n1;
n1 = n2;
if (n2)
n2 = n2->next;
}
*ppNode = n0;
}
void TestTopic1()
{
Node* list = NULL;
Node* tail;
PushBack(&list, 1);
PushBack(&list, 2);
PushBack(&list, 3);
PushBack(&list, 4);
PrintList(list);
ReverseList(&list);
PrintList(list);
}
输出结果:
1234
4321