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

Reverse a singly linked list

时间:2015-05-07 16:35:52      阅读:81      评论: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){
13             return head;
14         }
15         ListNode *p1,*p2,*p3;
16         p1 = head;
17         p2 = p1->next;
18         if (p2 == NULL){
19             return head;
20         }
21         p3 = p2->next;
22         while (p2 != NULL){
23             p2->next = p1;
24             p1 = p2;
25             p2 = p3;
26             if (p3 != NULL){
27                 p3 = p3->next;
28             }
29         }
30         head->next = NULL;
31         return p1;
32         
33     }
34 };

 

Reverse a singly linked list

标签:

原文地址:http://www.cnblogs.com/george-cw/p/4484981.html

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