链表的逆置

时间:2020-06-30 23:28:21   收藏:0   阅读:77

链表的逆置

带头结点

void InvertList(LinkList &L) {
	Lnode *p = L->next;
	if (p == NULL) return;
	Lnode *q = p->next;
	while (q != NULL ) {
		p->next = q->next;
		q->next = L->next;
		L->next = q;
		q = p->next;
	}
}

不带头结点

void InsertList_withoutHead(LinkList &L) {
	Lnode *p = L;
	if(p == NULL) return;
	Lnode *q = p->next;
	while (q != NULL) {
		p->next = q->next;
		q->next = L;
		L = q;
		q = p->next;
	}
}

代码链接,包括头插入法,尾插入法,前插操作,后插操作等基本操作。

原文:https://www.cnblogs.com/wryy/p/13216003.html

评论(0
© 2014 bubuko.com 版权所有 - 联系我们:wmxa8@hotmail.com
打开技术之扣,分享程序人生!