/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
ListNode* reverseList1(ListNode* head) {
ListNode* p=head;
ListNode* pre=NULL;
while(p)
{
ListNode* pnext=p->next;
p->next=pre;
pre=p;
p=pnext;
}
return pre;
}
ListNode* reverseList(ListNode* head) {
ListNode*tmp = NULL;
ListNode*p = NULL;
ListNode*ahead=new ListNode(-1);
ahead->next=head;
if (head == NULL)
{
return NULL;
}
tmp = ahead->next;
while (tmp->next != NULL)
{
p = tmp->next;
tmp->next = p->next;
p->next = ahead->next;
ahead->next = p;
}
head=ahead->next;
delete ahead;
return head;
}
};
翻转链表
最新推荐文章于 2024-10-13 16:52:53 发布