• Leetcode Reorder List


    实现链表如下所示:

    Given a singly linked list LL0→L1→…→Ln-1→Ln,
    reorder it to: L0→LnL1→Ln-1→L2→Ln-2→…

    一开始想到一个n方的,就是每次找最后一个回调到相应位置,然后倒数第二个的next置为NULL,依次类推。果然超时。

    /**
     * Definition for singly-linked list.
     * struct ListNode {
     *     int val;
     *     ListNode *next;
     *     ListNode(int x) : val(x), next(NULL) {}
     * };
     */
    class Solution {
    public:
        void reorderList(ListNode *head)
        {
            if (!head || !head->next) return ;
            ListNode *cur, *last, *next_cur, *pre_last;
            last = head -> next;
            pre_last = head;
            while(last -> next)
            {
                pre_last = last;
                last = last -> next;
            }
            cur = head;
            next_cur = head -> next;
    
            while(cur -> next || cur -> next != last)
            {
                cur -> next = last;
                pre_last -> next = NULL;
                cur = next_cur;
                next_cur = cur -> next;
                last = cur -> next;
                pre_last = cur;
                while(last && last -> next)
                {
                    pre_last = last;
                    last = last -> next;
                }
            }
            return ;
        }
    };
    View Code

    也可以用map<int, ListNode*>来做吧应该,但用了其他空间了。下面是比较直接的没有用多余空间的解法。

    分成两半,后面一半反转,然后合并前后两半。

    需要注意的是,前面一半的最后一个记得赋值NULL,还有如果是奇数,那么前面一半应该多一个。自己随便举个1234和12345的例子画一下就知道怎么合并怎么分了。

    /**
     * Definition for singly-linked list.
     * struct ListNode {
     *     int val;
     *     ListNode *next;
     *     ListNode(int x) : val(x), next(NULL) {}
     * };
     */
    class Solution {
    public:
        void reorderList(ListNode *head)
        {
            if (!head || !head -> next) return ;
            ListNode *slow = head, *quick = head -> next;
            while(quick && quick -> next) // 分成两部分
            {
                slow = slow -> next;
                quick = quick -> next;
                if (!quick)
                    break;
                quick = quick -> next;
            }
            quick = slow -> next;
            ListNode *last = quick -> next;
            while(last) // 反转后半部
            {
                quick -> next = last -> next;
                last -> next = slow -> next;
                slow -> next = last;
                last = quick -> next;
            }
            quick = slow -> next;
            slow -> next = NULL;
    
            ListNode *next_cur = head -> next, *next_quick, *cur = head;
            while(cur && quick) // 合并两部分
            {
                cur -> next = quick;
                next_quick = quick -> next;
                quick -> next = next_cur;
                quick = next_quick;
                cur = next_cur;
                if (cur)
                    next_cur = cur -> next;
            }
        }   
    };
  • 相关阅读:
    微软小冰迎来了一个新姐妹:“欣小然”
    终极之战:Linux & Windows
    逆天!百度AI音箱重磅升级:最大梦想实现
    国货之光!百度飞桨与华为麒麟重磅合作
    4天如何完爆Kafka源码核心流程!
    免费P7架构师直播课!技术人员如何提升职场技能?
    ZooKeeper核心原理及应用场景
    IT自由职业者是怎么样的感受和体验
    系统梳理主流定时器算法实现的差异以及应用
    微服务架构中分布式事务实现方案怎样何取舍
  • 原文地址:https://www.cnblogs.com/higerzhang/p/4161835.html
Copyright © 2020-2023  润新知