剑指offer-----15、反转链表
1、题目描述
输入一个链表,反转链表后,输出新链表的表头。
2、分析
这道题之前在leetcode上遇见过。附上leetcode解析(https://blog.csdn.net/zl6481033/article/details/89158460)。
3、代码
/*
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* cur=pHead;
ListNode* pre=NULL;
while(cur!=NULL){
ListNode* temp=cur->next;
cur->next=pre;
pre=cur;
cur=temp;
}
return pre;
}
};
4、相关知识点
链表相关知识。

浙公网安备 33010602011771号