Reverse a singly linked list.
链表逆置,之前好像做过类似的Leetcode题目了。
代码语言:javascript复制/**
* 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* p = head;
ListNode* q = p->next;
p->next = NULL;
while(q)
{
ListNode* r = q->next;
q->next = p;
p = q;
q = r;
}
return p;
}
};