单链表的两种逆序算法

typedef struct Node { Node* next; int data; }NODE; //非递归算法 NODE* s_reverse_node(NODE* Head) { NODE* cur = Head; NODE* pre = NULL; NODE* nxt = cur->next; while (nxt != NULL) { cur->next = pre; pre = cur; cur = nxt; nxt = cur->next; } if (cur != NULL) { cur->next = pre; } return cur; } //递归算法 NODE* t_reverse_node(NODE* Head) { NODE *nxt; if (Head->next != NULL) { //递归操作 nxt = t_reverse_node( Head->next ); Head->next->next = Head; Head->next = NULL; return nxt; } return Head; }

你可能感兴趣的:(算法,struct,null)