题意:将链表到序。
思路:简单模拟。
/**
* 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 NULL;
ListNode *next = head;
ListNode *pre = NULL;
ListNode *now = head;
while(next) {
head = next;
next = next->next;
now->next = pre;
pre = now;
now = next;
}
return head;
}
};