首页 > 其他 > 详细

Reverse Linked List****

时间:2015-05-09 01:12:57      阅读:233      评论:0      收藏:0      [点我收藏+]

Reverse a singly linked list.

click to show more hints.

Hint:

A linked list can be reversed either iteratively or recursively. Could you implement both?

分析:p = p->next的位置很重要!!继续思考!

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

 

Reverse Linked List****

原文:http://www.cnblogs.com/amazingzoe/p/4489193.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!