剑指Offer-15-反转链表

题目描述

输入一个链表,反转链表后,输出新链表的表头。

Code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
/*
struct ListNode {
int val;
struct ListNode *next;
ListNode(int x) :
val(x), next(NULL) {
}
};*/
class Solution {
public:
ListNode* ReverseList(ListNode* pHead) {
ListNode* pre=0,*next=0;
while(pHead!=NULL){
next=pHead->next;//记录当前节点的next节点
pHead->next=pre;//反转当前节点的next指针
pre=pHead;//为下一节点记录节点
pHead=next;//后移
}
return pre;
}
};

----\(˙<>˙)/----赞赏一下吧~