题目:
给你单链表的头节点 head ,请你反转链表,并返回反转后的链表。
示例 1:
![](https://i-blog.csdnimg.cn/blog_migrate/6585cf541cc08af810928b6f6a992634.jpeg)
输入:head = [1,2,3,4,5]
输出:[5,4,3,2,1]
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode() : val(0), next(nullptr) {}
* ListNode(int x) : val(x), next(nullptr) {}
* ListNode(int x, ListNode *next) : val(x), next(next) {}
* };
*/
class Solution {
public:
ListNode* reverseList(ListNode* head) {
ListNode*s;
for(s=NULL;head;swap(head,s)){
swap(s,head->next);
}
return s;
}
};