zoukankan      html  css  js  c++  java
  • 对单链表进行重排序

    要求:形式如L0->L1->L2->L3-> ... ->Ln-1->Ln,要求重新排序成如下形式:L0->Ln->L1->Ln-1->……
    如给定链表为1->2->3->4->5->6,则重新排序后的结果为1->6->2->5->3->4

     思路:分为以下三步:   

    1. 寻找链表的中点,把链表分为两部分。如上面链表从3->4这个地方分开。   
    2. 把中点后面的部分倒序。如上面链表后半部分倒序为6->5->4。   
    3. 把前半部分跟后半部分重新组合。如上面链表按照两个部分一边一个元素的顺序组合结果为1->6->2->5->3->4,就得到了最终结果。
    struct Node {
        int data;
        Node *next;
    };
    
    Node* searchMid(Node* head) {
        Node *fast = head, *slow = head, *preSlow = head;
    
        while (fast != NULL && fast->next != NULL) {
            preSlow = slow;
    
            fast = fast->next->next;
            slow = slow->next;
        }
        preSlow->next = NULL;
        return slow;
    }
    
    Node* reverse(Node* head){
    
        //返回反转链表的首元结点的地址
        if (head == NULL || head->next == NULL)
            return head;
    
        Node* newhead = reverse(head->next); // 先反转后面的链表
        head->next->next = head;//再将当前节点(head)设置为其然来后面节点(head->next)的后续节点
        head->next = NULL;
        return newhead; // 此处返回的newhead,永远指向反转后链表的首元节点,不随着回朔而变化。
    }
    
    void reOrder(Node* head) {
        if (head == NULL || head->next == NULL) 
            return;
    
        Node* cur2 = searchMid(head);
        cur2 = reverse(cur2);
    
        Node * cur1 = head->next;
        Node* temp = NULL;
        //合并两个链表L1,L2 其中L1的长度<=L2的长度,|L2的长度-L1的长度|<=1
        while (cur1!=NULL&&cur1->next!=NULL) {
            temp = cur1->next;
            cur1->next = cur2;
            cur1 = temp;
    
            temp = cur2->next;
            cur2->next = cur1;
            cur2 = temp;
        }
        if (cur1 == NULL) head->next = cur2;
        else cur1->next = cur2;
    }
  • 相关阅读:
    java 学习之环境配置
    报表设计--分组报表-普通分组-纵向扩展
    报表设计--网格式报表
    报表设计--简单报表
    SDP平台操作视频
    页面设计--Tree目录树
    页面设计--Grid列表
    页面设计--RadioButton
    页面设计--CheckBoxList
    页面设计--TextBox
  • 原文地址:https://www.cnblogs.com/fuqia/p/10262246.html
Copyright © 2011-2022 走看看