逆置/反转单链表

来源:互联网 发布:晋中市教育网络平台 编辑:程序博客网 时间:2024/05/29 18:58
//单链表逆置
//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
原创粉丝点击