链表的逆转:
非递归的实现有两种:
(1)先去掉最后一个节点,再进行头插;
(2)相邻的两个节点直接转换。
直接转换:
struct Node
{
int data;
Node* next;
};
typedef struct Node Node;
Node* reverse_list(Node* head)
{
if(head->next == NULL)
return head;
Node* p1 = head;
Node* p2 = head->next;
p1->next = NULL;
Node* temp_ptr;
while(NULL != p2)
{
temp_ptr = p2->next;
p2->next = p1;
p1 = p2;
p2 = temp_ptr;
}
return p1;
}
加一个链接:http://blog.163.com/lichunliang1988116@126/blog/static/26599443201282083655446/