206. 反转链表

leetcode-206. 反转链表

灵活运用了双指针,一快一慢两个指针

翻转链表

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
class Solution {
public:
ListNode* reverseList(ListNode* head) {
ListNode* pre = NULL;
ListNode* cur = head;
ListNode* temp; //用临时节点储存cur的下个节点。
while(cur != NULL)
{
temp = cur->next;
cur->next = pre;
pre = cur;
cur = temp;
}
return pre;
}
};