反转一个链表:
示例:输入:1->2->3->4->5->NULL
输出:5->4->3->2->1->NULL
分析如下:(1)利用头插的思想
typedef struct ListNode Node;
struct ListNode* reverseList(struct ListNode* head){
Node* pCur = head;
Node* pNewHead = NULL;
while(pCur){
head = pCur->next;
pCur->next = pNewHead;
pNewHead = pCur;
pCur = head;
}
return pNewHead;
}
(2)利用尾插的思想
typedef struct ListNode Node;
struct ListNode* reverseList(struct ListNode* head){
Node* pPre = NULL;
Node* pCur = head;
Node* pNext = NULL;
while (pCur){
pNext = pCur->next;
pCur->next = pPre;
pPre = pCur;
pCur = pNext;
}
return pPre;
}
~bye~