和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) {
ListNode *ne=head,*cur=NULL;
while(ne){
ListNode *tep=ne->next;
ne->next=cur;
cur=ne;
ne=tep;
}
return cur;
}
};