您的位置:首页 > 其它

leetcode 206: Reverse Linked List

2015-08-09 15:26 295 查看
/**
* Definition for singly-linked list.
* struct ListNode {
*     int val;
*     ListNode *next;
*     ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
ListNode* reverseList(ListNode* head) {
if(!head)
return head;
ListNode* res=head;
helper(head,res);
return res;
}
ListNode* helper(ListNode* head,ListNode*& phead)
{
if(head->next==NULL)
{
phead=head;
return head;
}
ListNode* p=helper(head->next,phead);
p->next=head;
head->next=NULL;
return head;
}
};
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: