当前位置:   article > 正文

1.4 如何对链表进行重新排序_链表重新排序

链表重新排序

1.题目

给定链表L0->L1->L2…Ln-1>Ln,把链表重新排序为L0->Ln->L1->Ln-1>L2->Ln-2…
要求:1.在原链表结点上排序,即不能申请新结点
2.只能修改next域,不能修改数据域

2.代码

public class Sort {
    /**
     * 方法功能:找出链表的中间结点,并从中间断成两段
     * 输入参数:链表的头结点
     * 返回值:链表的中间结点
     */

    // 1.slow走一步,fast走两步
    public static Node findMiddleNode(Node head){

        if(head == null || head.next == null){
            return head;
        }

        Node slow = head;
        Node fast = head;
        Node slowPre = head;

        // 要想走两步
        while(fast != null && fast.next != null){
            slowPre = slow;
            slow = slow.next;
            fast = fast.next.next;
        }

        slowPre.next = null;
        return slow;
    }

    /**
     * 方法功能:对不带虚假结点的单链表进行翻转
     * 输入参数:链表的头结点
     */
    public static Node reverseNode(Node head){
         if(head == null || head.next == null){
             return head;
         }

         Node pre = null;
         Node nex = null;
         // null head nex , head和nex不需要考虑什么关系
         while(head != null){
             nex = head.next;
             head.next = pre;
             pre = head;
             head = nex;
         }
         return pre;
    }

    /**
     * 方法功能: 将两部分链表连接起来
     * 输入参数: 链表的假头结点
     */
    public static void reorder(Node head){
        if(head == null || head.next == null){
            return;
        }

        // 前半部分第一个链表结点
        Node cur1 = head.next;
        // 后半部分逆序的第一个链表结点
        Node mid = findMiddleNode(head.next);
        Node cur2 = reverseNode(mid);
        Node tmp = null;

        while(cur1.next != null){
            tmp = cur1.next;
            cur1.next = cur2;

            // 这里面只有cur1结点没用
            cur1 = tmp;

            tmp = cur2.next;
            cur2.next = cur1;
            cur2 = tmp;
        }
        cur1.next = cur2;
    }

    public static void main(String[] args) {
        // 假的头结点
        Node head = new Node(-1);
        Node cur = head;
        Node tmp = null;

        for(int i = 1; i < 8; i++){
            tmp = new Node(i);
            cur.next = tmp;
            cur = tmp;
        }

        System.out.println("排序前为:");

        for(cur = head.next; cur != null; cur = cur.next){
            System.out.print(cur.data + " ");
        }

        System.out.println("\n --------------------------------------------");
        System.out.println("排序后为:");
        Sort.reorder(head);
        for(cur = head.next; cur != null; cur = cur.next){
            System.out.print(cur.data + " ");
        }
    }
}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107

在这里插入图片描述

查找、逆序、合并的时间复杂度为O(n),n为链表的长度,空间复杂度为O(1)

3.解题思路

1.因为是单链表,所以只能往后移动,不能往前移动
2.所以找到中间结点,并且对后半部分进行逆序,然后合并前后两半部分。

4.一些自己的尝试

1.翻转链表
方法一:创建虚假链表头

class Node{
    int data;
    Node next = null;

    public Node(int data){
        this.data = data;
    }
}

public class LinkedListSort {
    public static Node reverseOrder(Node head){
    	if(head == null || head.next == null){
            return head;
        }
        // 创建一个假链表头,并指向head
        Node fakeNode = new Node(0);
        fakeNode.next = head;

        Node pre = head;
        Node cur = head.next;
        Node tmp1 = null;

        while(cur != null){
            pre.next = cur.next;
            tmp1 = fakeNode.next;
            fakeNode.next = cur;
            cur.next = tmp1;

            // 经过上次交换后,cur和pre已经交换位置
            cur = pre.next;
        }
        return fakeNode.next;
    }

    public static void main(String[] args) {
        Node head = new Node(1);
        Node fake = head;
        for(int i = 2; i < 7; i++){
            Node tmp = new Node(i);
            fake.next = tmp;
            fake = fake.next;
        }

        fake = LinkedListSort.reverseOrder(head);
        while(fake != null){
            System.out.print(fake.data + " ");
            fake = fake.next;
        }
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50

方法2:不带,就是
添加链接描述

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/繁依Fanyi0/article/detail/226623
推荐阅读
相关标签
  

闽ICP备14008679号