时间限制:1秒 空间限制:32768K 热度指数:408664
本题知识点: 链表
算法知识视频讲解
题目描述
输入一个链表,反转链表后,输出新链表的表头。
/*
struct ListNode {
int val;
struct ListNode *next;
ListNode(int x) :
val(x), next(NULL) {
}
};*/
class Solution {
public:
ListNode* ReverseList(ListNode* pHead) {
ListNode * head,*tail;
head=pHead;
pHead=NULL;
while(head){
tail=head->next;
head->next=pHead;
pHead=head;
head=tail;
}
return pHead;
}
};