思路:
设置多个指针,指向相关节点即可
代码:
/*
struct ListNode {
int val;
struct ListNode *next;
ListNode(int x) :
val(x), next(NULL) {
}
};*/
class Solution {
public:
ListNode* ReverseList(ListNode* pHead) {
if(pHead==NULL)
return NULL;
ListNode *prior=NULL,*cur=pHead,*next;
while(cur){
next=cur->next;
cur->next=prior;
prior=cur;
cur=next;
}
return prior;
}
};
网友评论