(相关资料图)

1、typedef struct ListNode{ListNode *next;Element data;}ListNode, *pList;这是我做的单链表逆序三个不同的算法。

2、2个递归的以及一个非递归的pList ReverseList( pList head ){if( !head || !(head->next) )return head;pList ph = ReverseList( head->next );head->next->next = head;head->next = NULL;return ph;}pList ReverseList( pList head , pList &tail ){if( !head || !(head->next) ){tail = head;return head;}pList pt;pList ph = ReverseList( head->next , pt );pt->next = head;head->next = NULL;tail = head;return ph;}pList ReverseListNonRec( pList head ){if( !head || !(head->next) )return head;pList h = NULL,h1 = head;while( head ){h1 = head->next;head->next = h;h = head;head = h1;}return h;}。

本文到此分享完毕,希望对大家有所帮助。

推荐内容