算法实验室-19-链表重排(链表拆分,翻转,合并的综合应用)

原题:来自于牛客的经典必刷题第8

原理还是比较简单的

1.将一个链表拆分成两段

2.将链表翻转

3.将链表进行合并

基本代码:

    public void reorderList(ListNode head) {
        
        if(head == null||head.next == null){
            
            return;
        }
        //将链表从中间截断成两截
        ListNode[] segements = splitList(head);

        //将第二段L链表进行翻转
        ListNode l2 = segements[1];

        ListNode l2r = reverse(l2);

        ListNode node =  merge(segements[0],l2r);
        head.next = node.next;
    }

 实现细节:

    public void reorderList(ListNode head) {
        
        if(head == null||head.next == null){
            
            return;
        }
        //将链表从中间截断成两截
        ListNode[] segements = splitList(head);

        //将第二段L链表进行翻转
        ListNode l2 = segements[1];
        ListNode l2r = reverse(l2);
        ListNode node =  merge(segements[0],l2r);
        head.next = node.next;
    }
    //将链表从中间进行截断,返回第一段和第二段
    //将链表从中间进行截断,返回第一段和第二段
    public ListNode[] splitList(ListNode head){
        ListNode[] temps = new ListNode[2];
        ListNode faster = head;
        ListNode lower = head;
        while(faster!= null&&faster.next!=null&&faster.next.next!=null){
            lower = lower.next;
            faster = faster.next.next;
        }

        //分割成两段
        ListNode cache = lower.next;
        lower.next = null;
        //第一段从head~lower
        temps[0] = head;
        //第二段从cache ~ end
        temps[1] = cache;
        return temps;
    }

    public ListNode reverse(ListNode head){

        ListNode cur = head;
        ListNode pre = null;
        while(cur!= null){

            ListNode cache = cur.next;
            cur.next = pre;
            pre = cur;
            cur = cache;

        }
        return pre;
    }
    
    public ListNode merge(ListNode l1,ListNode l2){

        ListNode cur1 = l1;
        ListNode cur2 = l2;
        ListNode newHead = new ListNode(0);
        ListNode cur = newHead;
        while(cur1!= null&&cur2!= null){

            ListNode cacheCur1 = cur1.next;
            ListNode cacheCur2 = cur2.next;
            cur1.next = null;
            cur2.next = null;
            cur.next = cur1;
            cur.next.next = cur2;
            cur = cur.next.next;
            cur1 = cacheCur1;
            cur2 = cacheCur2;
        }
        if(cur1!= null){

            ListNode cache = cur1.next;
            cur1.next = null;
            cur.next = cur1;
            cur1 = cache;
        }
        if(cur2!= null){

            ListNode cache = cur2.next;
            cur2.next = null;
            cur.next = cur2;
            cur2 = cache;
        }
        return newHead.next;

    }

这道题主要是对基本链表:拆分,翻转,合并的综合应用,本身逻辑比较简单(至少比正道的后序遍历理解起来更简单,正道后序遍历很烧脑,给三颗星吧)

 

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值