题解 | #反转链表#
反转链表
https://www.nowcoder.com/practice/75e878df47f24fdc9dc3e400ec6058ca
暴力头插
struct ListNode* ReverseList(struct ListNode* pHead ) {
if(!pHead)
{
return pHead;
}
struct ListNode *p1=pHead->next;
struct ListNode *p2=NULL;
pHead->next=NULL;
while(p1)
{
p2=p1->next;
p1->next=pHead;
pHead=p1;
p1=p2;
}
return pHead;
// write code here
}
struct ListNode* ReverseList(struct ListNode* pHead ) {
if(!pHead)
{
return pHead;
}
struct ListNode *p1=pHead->next;
struct ListNode *p2=NULL;
pHead->next=NULL;
while(p1)
{
p2=p1->next;
p1->next=pHead;
pHead=p1;
p1=p2;
}
return pHead;
// write code here
}