• 【LeetCode】143. Reorder List


    Reorder List

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

    You must do this in-place without altering the nodes' values.

    For example,
    Given {1,2,3,4}, reorder it to {1,4,2,3}.

    解题思路:

    step1:将链表均分成两半。(即用fast&slow指针找出中间节点,切断即可。)

    step2:将后半链表逆序。

    step3:交替插入。

    /**
     * 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 == NULL || head->next == NULL)
                return;
    
            ListNode* fast = head;
            ListNode* slow = head;
            ListNode* preslow = NULL;
            while(fast != NULL)
            {
                fast = fast->next;
                if(fast != NULL)
                {
                    fast = fast->next;
                    preslow = slow;
                    slow = slow->next;
                }
            }
            ListNode* newhead = new ListNode(-1);
            ListNode* newtail = newhead;
    
            ListNode* head1 = head;
            ListNode* head2 = reverse(slow);
            preslow->next = NULL;
    
            while(head1 != NULL && head2 != NULL)
            {
                newtail->next = head1;
                head1 = head1->next;
                newtail = newtail->next;
    
                newtail->next = head2;
                newtail = newtail->next;
                head2 = head2->next;
            }
            if(head1 != NULL)
                newtail->next = head1;
            if(head2 != NULL)
                newtail->next = head2;
            head = newhead->next;
        }
        ListNode* reverse(ListNode* head)
        {
            if(head == NULL)
                return NULL;
            else if(head->next == NULL)
                return head;
            else if(head->next->next == NULL)
            {
                ListNode* tail = head->next;
                tail->next = head;
                head->next = NULL;
                return tail;
            }
            else
            {
                ListNode* pre = head;
                ListNode* cur = pre->next;
                ListNode* post = cur->next;
                while(post != NULL)
                {
                    cur->next = pre;
                    pre = cur;
                    cur = post;
                    post = post->next;
                }
                cur->next = pre;
                head->next = NULL;
                return cur;
            }
        }
    };

  • 相关阅读:
    LINUX 逻辑地址、线性地址、物理地址和虚拟地址 转
    Linux开机执行顺序
    Linux内核分析笔记 与Linux内核开发理论
    理解Linux系统中的load average(图文版)转
    Linux文件空洞与稀疏文件 转
    Linux中断(interrupt)子系统之一:中断系统基本原理 (图解)
    LINUX 内存结构
    LINUX 数据结构 &算法 网络协议 & 网络编程 多任务编程
    IO端口和IO内存的区别 转
    Linux内核高端内存 转
  • 原文地址:https://www.cnblogs.com/ganganloveu/p/4065262.html
Copyright © 2020-2023  润新知