您的位置:首页 > 其它

链表:反转、合并

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/
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: