leetcode 206

206. Reverse Linked List

Reverse a singly linked list.

翻转一个单链表。

代码如下:

 

 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* pre = NULL;
13         ListNode* cur = head;
14         while(cur != NULL)
15         {
16             ListNode* temp = cur->next;
17             cur->next = pre;
18             pre = cur;
19             cur = temp;
20         }
21         return pre;
22     }
23 };

 

 

 

posted @ 2016-11-14 16:02  花椰菜菜菜菜  阅读(165)  评论(0编辑  收藏  举报