码迷,mamicode.com
首页 > 其他好文 > 详细

单向链表逆转方法

时间:2018-06-09 20:33:31      阅读:111      评论:0      收藏:0      [点我收藏+]

标签:temp   source   ret   struct   介绍   span   data   head   遍历   

这里介绍增加内存的方法来逆转。利用新内存,复制每个节点的数据,再将头指针的next指向新的头。如此循环遍历原链。

struct data{
    data *next;
    int m_val;
};

data
* reverse(data *head){ data *new_chain == NULL; data *temp = NULL; while(head != NULL){ temp = (data*)malloc(sizeof(data)); //copy resource temp->m_val = head->m_val; if(new_chain == NULL){ new_chain = temp; }else{ temp->next=new_chain; new_chain = temp; }
    head = head->next; }
return new_chain; }

 

单向链表逆转方法

标签:temp   source   ret   struct   介绍   span   data   head   遍历   

原文地址:https://www.cnblogs.com/hustcpp/p/9160572.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!