• 链表的边界


    首先我们的链表题目 都是分为两种,一种是使用容器,但是另外一种就是不使用容器的.

    那么我们接下来的这些问题的都会根据这两种策略进行分析.

    问题一:输入链表头节点,奇数长度返回中点,偶数长度返回上中点.

    首先如果我们先进行分析使用容器的策略

    首先我们先分析

    如果我们将各个节点都放进数组中,那么我们的中点问题可以根据数组的下标进行解决.

    1->2->3->4->5

    节点3 的下标就是2,那么我们符合我们这个下标的计算公式就是-->(N/2)或者((N-1)/2)

    这个只是奇数的求解方式.

    但是我们的还是要就如果是偶数个数节点那么就取上节点

    1->2->3->4->5->6

    如果要求上节点那么就只能取到 ->(N-1)/2

    所以:

    1. public static Node midOrUpNode(Node head){
    2. if(head==null ||head.next==null){
    3. return head;
    4. }
    5. ArrayList list = new ArrayList<>();
    6. while(head!=null){
    7. list.add(head);
    8. head = head.next;
    9. }
    10. int N = list.size();
    11. return list.get((N-1)>>1);
    12. }

    同时我们不使用容器的策略就是使用快慢指针.

    1. public static Node midOrUpMidNode(Node head){
    2. if (head == null || head.next == null || head.next.next == null) {
    3. return head;
    4. }
    5. // 链表有3个点或以上
    6. Node slow = head;
    7. Node fast = head;
    8. while (fast.next != null && fast.next.next != null) {
    9. slow = slow.next;
    10. fast = fast.next.next;
    11. }
    12. return slow;
    13. }

    问题二:输入链表头结点,奇数长度返回中点,偶数长度返回下中点.

    容器解法:

    1. public static Node midOrDownNode(Node head){
    2. if(head==null || head.next==null){
    3. return head;
    4. }
    5. ArrayList list = new ArrayList<>();
    6. while(head!=null){
    7. list.add(head);
    8. head = head.next;
    9. }
    10. int N = list.size();
    11. return list.get(N>>1);
    12. }

    快慢指针解法:

    1. public static Node middleOrDown(Node head){
    2. if(head==null || head.next==null){
    3. return head;
    4. }
    5. if(head.next.next==null){
    6. return head.next;
    7. }
    8. Node fast = head.next;
    9. Node slow = head.next;
    10. while(fast.next!=null && fast.next.next!=null){
    11. fast = fast.next.next;
    12. slow = slow.next;
    13. }
    14. return slow;
    15. }

    问题二:输入链表头结点,奇数长度返回中点的前一个,偶数长度返回上节点的前一个

    容器解法:

    1. public static Node midOrUpMidprevNode(Node head){
    2. if(head==null || head.next==null){
    3. return head;
    4. }
    5. ArrayList list = new ArrayList<>();
    6. while(head!=null){
    7. list.add(head);
    8. head = head.next;
    9. }
    10. int N = list.size();
    11. return list.get((N-3)>>1);
    12. }

    快慢指针:

    1. public static Node midOrUpPrevNode(Node head){
    2. if(head==null || head.next==null || head.next.next==null){
    3. return null;
    4. }
    5. Node fast = head.next.next;
    6. Node slow = head;
    7. while(fast.next!=null && fast.next.next!=null){
    8. slow = slow.next;
    9. fast = fast.next.next;
    10. }
    11. return slow;
    12. }

    问题四:输入链表头结点,奇数长度返回中点的前一个,偶数长度返回下中点的前一个

    容器:

    1. public static Node midOrMidprevNode(Node head){
    2. if(head==null || head.next==null || head.next.next==null){
    3. return head;
    4. }
    5. ArrayList list = new ArrayList<>();
    6. while(head!=null){
    7. list.add(head);
    8. head = head.next;
    9. }
    10. int N = list.size();
    11. return list.get((N-2)>>1);
    12. }

    快慢指针:

    1. public static Node midOrDownMidprev(Node head){
    2. if(head==null || head.next==null){
    3. return null;
    4. }
    5. if(head.next.next==null){
    6. return head;
    7. }
    8. Node fast = head.next;
    9. Node slow = head;
    10. while(fast.next!=null && fast.next.next!=null){
    11. fast = fast.next.next;
    12. slow = slow.next;
    13. }
    14. return slow;
    15. }

    总结:链表中点问题的边界条件不可以硬背,什么时候用,什么时候推.

  • 相关阅读:
    SkyWalking追踪gateway网关链路无法和服务串联
    【Elasticsearch】Elasticsearch命令行操作
    【UV打印机】波形开发-矢量波形工具(五)
    jpa分页插件对象Pageable出现了错误异常如何解决?
    如何分析Apple搜索广告效果
    【光学】Matlab实现迈克尔逊干涉仪动态仿真
    如何优化供应商采购系统,提升供应商管理和采购流程效能
    prim生成树
    有一个项目管理软件,名字叫8Mnaage PM!
    java:aocache 与Spring Aop兼容问题
  • 原文地址:https://blog.csdn.net/weixin_61652218/article/details/126672899