week2

ListNode* removeNthFromEnd(ListNode* head, int n) {
        ListNode *dummy=new ListNode(-1);
        dummy->next=head;
        ListNode *first=dummy;
        ListNode *second=dummy;
        for(int i=0;i<n;i++)
            first=first->next;
        while(first->next!=NULL)
        {
            first=first->next;
            second=second->next;
        }
        second->next=second->next->next;
        return dummy->next;

    }
void deleteNode(ListNode* node) {
        node->val=node->next->val;
        node->next=node->next->next;
    }
ListNode* deleteDuplicates(ListNode* head) {
        auto cur=head;
        while(cur)
        {
            if(cur->next && cur->next->val==cur->val)
                cur->next=cur->next->next;
            else cur=cur->next;
        }
        
        return head;

    }
ListNode* rotateRight(ListNode* head, int k) {
        if(head==NULL)
            return head;

        int n=0;
        for(auto p=head;p!=NULL;p=p->next)
                n++;
        k%=n;

        ListNode *first=head;
        ListNode *second=head;
        while(k--)   first=first->next;
        while(first->next!=NULL)
        {
            first=first->next;
            second=second->next;
        }
        first->next=head;
        head=second->next;
        second->next=NULL;
        return head;
    }
ListNode* swapPairs(ListNode* head) {
        ListNode* dummy=new ListNode(-1);
        dummy->next=head;
        for(auto p=dummy;p->next && p->next->next;)
        {
            auto a=p->next,b=p->next->next;
            p->next=b;
            a->next=b->next;
            b->next=a;
            p=a;
        }
        return dummy->next;
    }
ListNode* reverseList(ListNode* head) {
        auto dummy=new ListNode(-1);
        auto pre=head;
        auto temp=head;
        while(pre!=NULL)
        {
            temp=pre->next;
            pre->next=dummy->next;
            dummy->next=pre;
            pre=temp;
        }

    return dummy->next;


    }
ListNode* reverseBetween(ListNode* head, int m, int n) {
        if(m==n)  return head;
        auto dummy=new ListNode(-1);
        dummy->next=head;
        auto a=dummy,d=dummy;
        for(int i=0;i<m-1;i++)
                a=a->next;
        for(int i=0;i<n;i++)
                d=d->next;
        auto b=a->next,c=d->next;
        for(auto p=b,q=b->next;q!=c;)
        {
            auto o=q->next;
            q->next=p;
            p=q,q=o;
        }
        b->next=c;
        a->next=d;

        return dummy->next;
    }
ListNode *getIntersectionNode(ListNode *headA, ListNode *headB) {
        auto p=headA,q=headB;
        while(p!=q)
        {
            if(p) p=p->next;
            else p=headB;

            if(q) q=q->next;
            else q=headA;
        }
        return p;
    }
ListNode *detectCycle(ListNode *head) {
        auto fast=head,slow=head;
        while(fast)
        {
            fast=fast->next;
            slow=slow->next;
            if(fast) fast=fast->next;
            else break;
            if(fast==slow)
            {
                slow=head;
                while(fast!=slow)
                {
                    fast=fast->next;
                    slow=slow->next;
                }
                return slow;
            }


        }
        return NULL;
    }
ListNode* sortList(ListNode* head) {
       int n = 0;
        for (auto p = head; p; p = p->next) n ++ ;

        auto dummy = new ListNode(-1);
        dummy->next = head;
        for (int i = 1; i < n; i *= 2) {
            auto cur = dummy;
            for (int j = 1; j + i <= n; j += i * 2) {
                auto p = cur->next, q = p;
                for (int k = 0; k < i; k ++ ) q = q->next;
                int x = 0, y = 0;
                while (x < i && y < i && p && q) {
                    if (p->val <= q->val) {cur->next=p;cur=cur->next; p=p->next; x++;}
                    else {cur->next=q;cur=cur->next;q=q->next;y++;}
                }
                while (x < i && p) {cur->next=p;cur=cur->next; p=p->next; x++;}
                while (y < i && q) {cur->next=q;cur=cur->next;q=q->next;y++;}
                cur->next = q;
            }
        }
        return dummy->next;
    }

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值