面试题24:反转链表
题目:定义一个函数,输入一个链表的头结点,反转该链表并输出反转后链表的头结点。
#include <iostream>
#include <algorithm>
#include <cmath>
#include <cstring>
using namespace std;
struct ListNode{
int value;
ListNode* next;
};
ListNode* ReverseList(ListNode* pHead){
ListNode* pReversedHead=NULL;
ListNode* pNode=pHead;
ListNode* pPrev=NULL;
while(pNode!=NULL){
ListNode* pNext=pNode->next;
if(pNext==NULL) pReversedHead=pNode;
pNode->next=pPrev;
pPrev=pNode;
pNode=pNext;
}
return pReversedHead;
}
int main() {
return 0;
}