头插法翻转链表

 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 };

 

posted on 2018-04-02 22:03  CreatorKou  阅读(103)  评论(0编辑  收藏  举报

导航