/**
* struct ListNode {
* int val;
* struct ListNode *next;
* };
*/
/**
*
* @param pHead ListNode类
* @return ListNode类
*/
//方法一;指针颠倒
/*
struct ListNode* ReverseList(struct ListNode* pHead ) {
// write code here
struct ListNode *pre=NULL ,*cur;
cur=pHead;
if(pHead==NULL||pHead->next==NULL)
{
return pHead;
}
else
{
while(cur->next!=NULL)
{
struct ListNode *temp;
temp=cur->next;
cur->next=pre;
pre=cur;
cur=temp;
//cur=cur->next;
}
cur->next=pre;
pre=cur;
}
return pre;
}
*/
//方法二:依次取出结点,并且头插
struct ListNode* ReverseList(struct ListNode* pHead )
{
if(pHead==NULL||pHead->next==NULL)
{
return pHead;
}
else
{
struct ListNode *pre,*cur;
cur=pHead;
struct ListNode *first=NULL;
while(cur->next!=NULL)
{
struct ListNode *temp;
if(first==NULL)
{
temp=cur->next;
cur->next=first;
first=cur;
cur=temp;
}
else
{
temp=cur->next;
cur->next=first;
first=cur;
cur=temp;
}
}
cur->next=first;
first=cur;
return first;
}
}