单链表的逆转
2017-04-05 本文已影响0人
树林里的小怪兽
方法一
L *link(L *head)
{
L *p, *q;
p = head->next;
head - >next = NULL;
while ( q ! = NULL)
{
q = p->next;
p = head->next;
head->next = p;
p = q;
}
return head;
}
将原先的链表节点摘取下来,使用头结点插入的方法再插入,最后实现单链表的逆序
方法二
L *link(L *head)
{
L *pre , *p = head->next, *r = p->next;
p->next = NULL;
while (r != NULL)
{
pre = p;
p=r;
r = r->next;
p->next = pre;
}
head->next = p
return head;
}