头插法翻转链表

 1 /*
 2 struct ListNode {
 3     int val;
 4     struct ListNode *next;
 5     ListNode(int x) :
 6             val(x), next(NULL) {
 7     }
 8 };*/
 9 class Solution {
10 public:
11     ListNode* ReverseList(ListNode* pHead) {
12         ListNode *tmp = pHead,*cur;
13         ListNode *hnode = new ListNode(0);
14         while(tmp){
15             cur = tmp;
16             tmp = tmp->next;
17             cur->next = hnode->next;
18             hnode->next = cur;
19         }
20         return hnode->next;
21     }
22 };
原文地址:https://www.cnblogs.com/CreatorKou/p/8698127.html