zoukankan      html  css  js  c++  java
  • 【Lintcode】099.Reorder List

    题目:

    Given a singly linked list L: L0 → L1 → … → Ln-1 → Ln

    reorder it to: L0 → Ln → L1 → Ln-1 → L2 → Ln-2 → …

    Example

    Given 1->2->3->4->null, reorder it to 1->4->2->3->null.

    题解:

       Spliting the list from the middle into two lists. One from head to middle, and the other from middle to the end. Then we reverse the second list. Finally we merge these two lists

    Solution 1 ()

    class Solution {
    public:
        void reorderList(ListNode *head) {
            if (!head || !head->next) {
                return;
            }
            ListNode* mid = findMiddle(head);
            ListNode* right = reverse(mid->next);
            mid->next = nullptr;
            merge(head, right);
        }
        ListNode* findMiddle(ListNode* head) {
            ListNode* slow = head;
            ListNode* fast = head->next;
            while (fast && fast->next) {
                slow = slow->next;
                fast = fast->next->next;
            }
            
            return slow;
        }
        ListNode* reverse(ListNode* head) {
            if (!head || !head->next) {
                return head;
            }
            
            ListNode* pre = nullptr;
            while (head) {
                ListNode* tmp = head->next;
                head->next = pre;
                pre = head;
                head = tmp;
            }
            return pre;
        }
        void merge(ListNode* left, ListNode* right) {
            ListNode* dummy = new ListNode(-1);
            int idx = 0;
            while (left && right) {
                if (idx % 2 == 0) {
                    dummy->next = left;
                    left = left->next;
                } else {
                    dummy->next = right;
                    right = right->next; 
                }
                dummy = dummy->next;
                ++idx;
            }    
            if (left) {
                dummy->next = left;
            } else {
                dummy->next = right;
            }
        }
    };

      from here

    Solution 2 ()

    class Solution {
    public:
        /**
         * @param head: The first node of linked list.
         * @return: void
         */
        void reorderList(ListNode *head) {
            // write your code here
            if (head == NULL)
                return;
            
            vector<ListNode*> nodes;
            ListNode* iter = head;
            while(iter != NULL)
            {
                nodes.push_back(iter);
                iter = iter->next;
            }
            
            int LEN = nodes.size();
            int left = 0;
            int right = LEN -1;
            while(left < right)
            {
                nodes[left]->next = nodes[right];
                nodes[right--]->next = nodes[++left];
            }
            nodes[left]->next = NULL;
        }
    };
  • 相关阅读:
    netstat命令
    为什么 netstat 对某些服务只显示了 tcp6 监听端口
    端口状态说明 LISTENING、ESTABLISHED、TIME_WAIT及CLOSE_WAIT
    进程启动时主线程创建过程分析
    [Kali]关机卡死,google拼音无法输入
    [白帽子讲WEB安全]XSS <Cross Site Script>
    [白帽子将WEB安全笔记]浏览器安全
    [白帽子将WEB安全笔记]我的安全世界观
    mongodb高可用集群 3 ---分片与副本集结合
    python计算年龄小程序
  • 原文地址:https://www.cnblogs.com/Atanisi/p/6848336.html
Copyright © 2011-2022 走看看