设置链表数据结构
typedef struct Node* List;
struct Node{
int Data;
List Next;
};
反转链表,且不占用额外空间
void Reverse(List &L) {
List pre, next;
pre = NULL;
while (L) {
next = L->Next;
L->Next = pre;
pre = L;
L = next;
}
L = pre;
}
反转链表,返回反转后的结果,原链表不受影响
List Reverse(List L) {
List t;
t = NULL;
while (L) {
List p = (List)malloc(sizeof(List));
p->Data = L->Data;
p->Next = t;
L = L->Next;
t = p;
}
return t;
}
反转链表,返回翻转后的结果,原链表移到最后一个结点
List Reverse(List L) {
PtrToNode t = NULL;
PtrToNode L2 = NULL;
while (L != NULL) {
t = L->Next;
L->Next = L2;
L2 = L;
L = t;
}
return L2;
}