原题链接 https://leetcode.com/problems/reverse-linked-list/
Reverse a singly linked list.
反转链表
水过。
/**
* 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 == NULL)return NULL;
ListNode* tmp = head;
ListNode* current = tmp;
tmp = tmp->next;
current->next = NULL;
while (tmp != NULL)
{
ListNode* t = tmp->next;
tmp->next = current;
current = tmp;
tmp = t;
}
return current;
}
};