链表的逆置
链表的逆置
带头结点
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;
}
}
代码链接,包括头插入法,尾插入法,前插操作,后插操作等基本操作。
Don't aim for success if you really want it.Just stick to what you love and believe in.And it will come naturally.