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;
}
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;
}
相关文章推荐
- LeetCode 206. Reverse Linked List
- LeetCode - 206. Reverse Linked List
- [LeetCode]206. Reverse Linked List
- 每天一道LeetCode--206. Reverse Linked List
- LeetCode *** 206. Reverse Linked List
- [leetcode]206. Reverse Linked List
- leetcode 206. Reverse Linked List
- 【LeetCode】206. Reverse Linked List
- [LeetCode]206. Reverse Linked List
- LeetCode-206. Reverse Linked List
- 【LeetCode】206. Reverse Linked List
- 【leetcode】206. Reverse Linked List My Submissions Question
- [leetcode]206. Reverse Linked List
- 【leetcode】206.Reverse Linked List
- LeetCode 206. Reverse Linked List
- LeetCode 206. Reverse Linked List
- [LeetCode] 206. Reverse Linked List
- LeetCode 206. Reverse Linked List
- 【LeetCode】206. Reverse Linked List
- 206. Reverse Linked List leetcode list