206
链表逆置
原地逆置和额外空间o(1)的差别就是
ListNode *new_head=new ListNode();
----->ListNode *new_head=NULL;
new_head->next
----->new_head;
class Solution {
public:
ListNode* reverseList(ListNode* head) {
if(head==NULL||head->next==NULL)
return head;
//ListNode *new_head=new ListNode();
ListNode *new_head=NULL;
ListNode *p;
while(head)
{
p=head->next;
//head->next=new_head->next;
head->next=new_head;
//new_head->next=head;
new_head=head;
head=p;
}
return new_head;
}
};