leetcode206链表逆置

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;

    }
};
原创文章 23 获赞 26 访问量 435

猜你喜欢

转载自blog.csdn.net/weixin_41672404/article/details/105930492