您的位置:首页 > 其它

LeetCode 206. Reverse Linked List

2017-02-02 16:06 519 查看
解题思路: 使用两个指针,分别指向head 和 head->next,或者直接递归.

class Solution {
public:
ListNode* reverseList(ListNode* head) {
ListNode* p1 = NULL;
ListNode* p2 = NULL;
while(head){
p1 = head->next;
head->next = p2;
p2 = head;
head = p1;
}
return p2;
}
};

ListNode* reverseList(ListNode* head) {
if(!head || !(head->next)) return head;
ListNode* res = reverseList(head->next);
head->next->next = head;
head->next = NULL;
return res;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: