# 【链表】非递归过程以O(N)反转单链表

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>

struct Node;
typedef Node *PtrToNode;
typedef PtrToNode List;
typedef PtrToNode Position;

struct Node
{
int val;
Position Next;
};

void Insert(int val, List list, Position pos)
{
Node *p;
p = (Node *)malloc(sizeof(Node));
p->val = val;
p->Next=NULL;
pos->Next = p;
}

//数据结构与算法分析 习题3.12
List ReverseList(List L) {
Position CurrentPos, NextPos, PreviousPos;
PreviousPos = NULL;
CurrentPos = L; NextPos = L->Next;
while (NextPos != NULL) {
CurrentPos->Next = PreviousPos;
PreviousPos = CurrentPos;
CurrentPos = NextPos;
NextPos = NextPos->Next;
}
CurrentPos->Next = PreviousPos;
return CurrentPos;
}

void main()
{
List list, p;
Position pos,l;
p = (Node *)malloc(sizeof(Node));
pos = p;
for (int i = 1; i <= 10; i++) {
Insert(i, p, pos);
pos = pos->Next;
}
l = p->Next;
while (l) {
printf("%d ", l->val);
l = l->Next;
}
printf("\n");
list = p->Next;
list = ReverseList(list);
l = list;
while (l) {
printf("%d ", l->val);
l = l->Next;
}
getchar();
}