题目:
反转链表
题目描述:
输入一个链表,反转链表后,输出链表的所有元素。
解题:
/*
struct ListNode {
int val;
struct ListNode *next;
ListNode(int x) :
val(x), next(NULL) {
}
};*/
class Solution {
public:
ListNode* ReverseList(ListNode* pHead) {
ListNode *pre = NULL, *last;
while(pHead){
last = pHead->next;
pHead->next = pre;
pre = pHead;
pHead = last;
}
return pre;
}
};