Leetcode 206 Reverse Linked List 链表

将单向链表反转

完成如图操作,依次进行即可

1

2

3

 1 /**
 2  * Definition for singly-linked list.
 3  * struct ListNode {
 4  *     int val;
 5  *     ListNode *next;
 6  *     ListNode(int x) : val(x), next(NULL) {}
 7  * };
 8  */
 9 class Solution {
10 public:
11     ListNode* reverseList(ListNode* head) {
12         ListNode* now = head;
13         head = NULL;
14         while(now){
15             ListNode* tnow= now;
16             now = now->next;
17             tnow->next = head;
18             head = tnow;
19         }
20         return head;
21     }
22 };

 

posted @ 2016-01-25 22:40  Breeze0806  阅读(169)  评论(0编辑  收藏  举报