每日一练 数据结构
一、题目描述
二、解题步骤
代码如下(示例):
/*
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 pHead;
ListNode* pre = NULL;
ListNode* cur = pHead;
while(cur != NULL) {
ListNode* tmp = cur->next;
cur->next = pre;
pre = cur;
cur = tmp;
}
return pre;
}
};