码迷,mamicode.com
首页 > 其他好文 > 详细

[LeetCode] Reverse Linked List

时间:2015-09-03 16:29:02      阅读:186      评论:0      收藏:0      [点我收藏+]

标签:

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         if (head == NULL) return NULL;
13         
14         ListNode* p = head->next;
15         head->next = NULL;
16         
17         while (p != NULL) {
18             ListNode* q = p->next;
19             p->next = head;
20             head = p;
21             p = q;
22         }
23         
24         return head;
25     }
26 };

 

[LeetCode] Reverse Linked List

标签:

原文地址:http://www.cnblogs.com/vincently/p/4780233.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!