快慢指针的边界问题

输入链表头节点:要求如下 👇

          1.奇数长度返回中点,偶数长度返回上中点
          2.奇数长度返回中点,偶数长度返回下中点
          3.奇数长度返回中点前一个,偶数长度返回上中点前一个
          4.奇数长度返回中点前一个,偶数长度返回下中点前一个

        涉及链表的操作时"快慢指针"是最基础的技巧,对其边界的把握也要非常熟练!

package com.kali.link;

/**
 * 快慢指针:
 *      输入链表头节点:
 *          1.奇数长度返回中点,偶数长度返回上中点
 *          2.奇数长度返回中点,偶数长度返回下中点
 *          3.奇数长度返回中点前一个,偶数长度返回上中点前一个
 *          4.奇数长度返回中点前一个,偶数长度返回下中点前一个
 */
//TODO : FINISH IT !!!
public class LinkedListMidQuestion {
    public static void main(String[] args) {
        int[] arr = {23,12,1,4,65,67};
        MyLinked link = new MyLinked();
        link.add(arr);
        Node head = link.getHead();

        System.out.println(midUpMid(head));

        System.out.println(midDownMid(head));

        System.out.println(PreOfMidPreOfUpMid(head));

        System.out.println(PreOfMidPreOfDownMid(head));
    }

    /*1.奇数返回中点,偶数返回上中点*/
    public static Node midUpMid(Node head){
        if(head == null || head.next == null || head.next.next == null){
            return head;
        }
        //快慢指针
        Node slow = head;
        Node fast = head;
        // 1 1 1 1
        // 0 0 0 0 0 0 0 0
        // 1   1   1   1
        while(fast.next != null && fast.next.next != null){
            slow = slow.next;
            fast = fast.next.next;
        }
        return slow;
    }

    /*2.奇数返回中点,偶数返回下中点*/
    public static Node midDownMid(Node head){
        if(head == null){
            return null;
        }
        if(head.next == null || head.next.next == null){
            return head.next;
        }
        Node slow = head;
        Node fast = head.next;
        // 1 1 1 1
        // 0 0 0 0 0 0 0 0 0
        //   1   1   1   1
        while(fast.next != null && fast.next.next != null){
            slow = slow.next;
            fast = fast.next.next;
        }
        return slow.next;
    }

    /*3.奇数长度返回中点前一个,偶数长度返回上中点前一个*/
    public static Node PreOfMidPreOfUpMid(Node head){
        if(head == null || head.next == null || head.next.next == null){
            return null;
        }
        Node slow = head;
        Node fast = head.next.next;
        // 1 1 1
        // 0 0 0 0 0 0 0 0
        //     1   1   1
        while(fast.next != null && fast.next.next != null){
            slow = slow.next;
            fast = fast.next.next;
        }
        return slow;
    }

    /*4.奇数长度返回中点前一个,偶数长度返回下中点前一个*/
    public static Node PreOfMidPreOfDownMid(Node head){
        if(head == null || head.next == null){
            return null;
        }
        if(head.next.next == null){
            return head;
        }
        Node slow = head;
        Node fast = head.next;
        // 1 1 1 1
        // 0 0 0 0 0 0 0 0 0
        //   1   1   1   1
        while(fast.next != null && fast.next.next != null){
            slow = slow.next;
            fast = fast.next.next;
        }
        return slow;
    }
}


        慢指针始终从head开始,变化的只有快指针的起始位置,返回的也可能是慢指针的下一个节点,视情况而定。

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值