链表:反转、合并
2014-01-14 09:39
141 查看
关于链表的基础知识,请参考我的其他文档:http://blog.csdn.net/look595271601/article/details/13210725
在实现以下操作之前,还是首先定义基本数据类型:
[cpp] view
plaincopy
typedef struct Node
{
int data;
struct LinkList * next;
}LinkNode, *LinkList;
(1)链表的反转
基本思想:利用一个辅助指针(tmp),用于保存当前指针指向的下一个结点;然后,反转当前结点的指针指向,使其指向前一个结点;最后,利用辅助指针向后遍历。
[cpp] view
plaincopy
void ReverseList(LinkList &head)
{
LinkList pre, cur, tmp;
if(head == NULL)
return;
pre = head;
cur = head->next;
while(cur)
{
tmp = cur->next;
cur->next = pre;
pre = cur;
cur = tmp;
}
head->next = NULL; //将原头结点的指针,指向NULL,作为反转后链表的尾指针
head = pre; //此语句不能丢,一定要记得将头结点改变位置
}
(2)http://blog.csdn.net/rtyytr/article/details/6776014
(3)合并两个有序链表
http://blog.csdn.net/lalor/article/details/7429989 http://blog.163.com/zhaohai_1988/blog/static/2095100852012620102858311/
在实现以下操作之前,还是首先定义基本数据类型:
[cpp] view
plaincopy
typedef struct Node
{
int data;
struct LinkList * next;
}LinkNode, *LinkList;
(1)链表的反转
基本思想:利用一个辅助指针(tmp),用于保存当前指针指向的下一个结点;然后,反转当前结点的指针指向,使其指向前一个结点;最后,利用辅助指针向后遍历。
[cpp] view
plaincopy
void ReverseList(LinkList &head)
{
LinkList pre, cur, tmp;
if(head == NULL)
return;
pre = head;
cur = head->next;
while(cur)
{
tmp = cur->next;
cur->next = pre;
pre = cur;
cur = tmp;
}
head->next = NULL; //将原头结点的指针,指向NULL,作为反转后链表的尾指针
head = pre; //此语句不能丢,一定要记得将头结点改变位置
}
(2)http://blog.csdn.net/rtyytr/article/details/6776014
(3)合并两个有序链表
http://blog.csdn.net/lalor/article/details/7429989 http://blog.163.com/zhaohai_1988/blog/static/2095100852012620102858311/
相关文章推荐
- 合并两个有序链表/链表反转(逆置)/找链表倒数第k点(遍历一次)
- 剑指Offer:反转链表、合并两个排序的链表
- 链表的反转、合并(不借助额外list数组)
- 【剑指offer】第二十四题(反转链表) 和 第二十五题(合并两个有序链表)
- 关于链表结构的基本操作 c 实现 (创建,插入删除,反转,合并链表,查找,是否有环,链表相交情况)
- 链表的删除 合并 排序 反转
- C++单链表反转、两有序链表合并仍有序
- 剑指Offer面试题16反转链表(递归和非递归),面试题17合并两个排序的链表(递归)
- 剑指offer面试题17,18:反转链表+合并有序链表
- 单链表反转 - 有序链表合并 - 子树包含
- 【练习】单链表反转,合并
- 反转链表后输出、合并两个排序链表、树的子结构 -- 漫漫算法路 刷题篇
- 链表常用操作 (定义,创建,反转,合并,交叉)
- 链表:反转、合并
- 20140720 链表反转 、合并、二叉树镜像
- 反转单链表+合并有序单链表+查找单链表中倒数第k个节点--20150924
- 《剑指offer》学习之--反转链表与合并两个排序链表
- 单链表反转 合并 等例子
- 数据结构之链表面试题汇总(二)-有序单向链表的合并、单向链表反转
- 链表相关面试题目 反转 合并