单链表反序
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了单链表反序相关的知识,希望对你有一定的参考价值。
typedef struct node *linklist;
//revese linklist
linklist reverselinklist1(linklist l) {
linklist current, p;
if(l == NULL) {
return NULL;
}
current = l -> next;
while(current -> next != NULL) {
p = current -> next;
current -> next = p -> next;
p -> next = l -> next;
l -> next = p;
}
return l;
}
linklist reverselinklist2(linklist l) {
linklist current, p;
if(l == NULL) {
return NULL;
}
current = l -> next;
while(current -> next != NULL) {
p = current -> next;
l -> next = p;
current -> next = p -> next;
p -> next = current;
}
}
linklist reverselinklist3(linklist l) {
linklist newlist, temp;
newlist = (linklist)malloc(sizeof(struct node));
if(l == NULL || newlist == NULL) {
return NULL;
}
newlist -> data = l -> data;
newlist -> next = NULL;
while(l -> next != NULL) {
temp = newlist -> next;
newlist -> next = l -> next;
l -> next = l -> next -> next;
newlist -> next -> next = temp;
}
free(l);
return newlist;
}
以上是关于单链表反序的主要内容,如果未能解决你的问题,请参考以下文章