左程云_算法与数据结构 — 链表问题 — 03删除链表的中间节点和a/b处的节点

问题描述

给定链表的头节点head,实现删除链表的中间节点的方法;
若为偶数个:比如1234则删除节点2;
给定链表的头节点head,实现删除链表的a/b处节点的方法;

思路分析

删除中间节点

1个节点,不删除;
2个节点,删除节点1;
3个节点,删除节点2;
4个节点,删除节点2;
5个节点,删除节点3;
6个节点,删除节点3;
可以观察得出,在2个节点以上的情况下,后面每存在两个节点则被删除的节点往后移1;所以可以在k>2的时候进行判断:
while(current.next != null && current.next.next != null){
midNode = midNode.next;
current = current.next.next;
}

删除a/b处节点

给定整数a和b,实现删除位于a/b位置的节点;
打个比方:1->2->3->4->5 这个链表
如果在区间 (0,1/5], 删除节点1;
如果在区间 (0,2/5], 删除节点2;
如果在区间 (0,3/5], 删除节点3;
如果在区间 (0,4/5], 删除节点4;
如果在区间 (0,1], 删除节点5;
可以看出,是在r = ( a / b ) * n 然后将n向上取整得到的节点位置的方法;
所以根本是在于 求出 r的值并且运用Math.ceil()方法向上取整来得到准确的节点值,然后遍历链表删除即可;

代码实现1

package algorithm_zuochengyun;

public class CH2_03_removeByRatio {

    public static Node removeByRatio(Node head, int a, int b) {

        System.out.print("length = " + Node.getLen(head) + " ");
        int length = Node.getLen(head);
        int r = (int) Math.ceil((double) (length * a) / (double) b);
        System.out.println("r = " + r);
        Node current = head;
        while (--r > 1) {
            current = current.next;
        }
        current.next = current.next.next;
        return head;
    }

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Node head = Node.init();
        Node.Print(head);
        removeByRatio(head, 5, 7);
        Node.Print(head);
        removeByRatio(head, 3, 6);
        Node.Print(head);
    }

}

实现结果1

这里写图片描述

代码实现2

package algorithm_zuochengyun;

import java.util.logging.Logger;

public class CH2_03_removeMidNode {

    public static Node removeMidNode(Node head) {
        if (head == null || head.next == null) {
            System.out.println("null or 1 node .");
            return head;
        } else if (head.next != null && head.next.next == null) {
            System.out.println("2 nodes .");
            return head.next;
        } else {
            System.out.println("at least 3 nodes .");
            // 起码三个 口(pre)口口(current),口口,口口
            Node delete_preNode = head;
            Node currentNode = head.next.next;
            // 如果每当current后面都有两个几点结点存在那么被删除的结点则会往后推一个
            // 如果不存在current后两个结点或者只存在curren.next那么仍然删除之前该删除的那个结点
            while (currentNode.next != null && currentNode.next.next != null) {
                currentNode = currentNode.next.next;
                delete_preNode = delete_preNode.next;
            }
            delete_preNode.next = delete_preNode.next.next;
        }
        return head;
    }

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Node head1 = Node.init();
        Node.Print(head1);
        Node.Print(removeMidNode(head1));
    }

}

实现结果2

这里写图片描述

问题总结

要学会从事件中寻找到规律
要透过描述看问题的本质要求的东西
Math类中取整的不同方法
  1. Math.floor() 向下取整
  2. Math.ceil() 向上取整
  3. Math.round() 四舍五入取整
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值