题解 | #从单向链表中删除指定值的节点#

从单向链表中删除指定值的节点

http://www.nowcoder.com/practice/f96cd47e812842269058d483a11ced4f


public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        while (sc.hasNext()) {
            int n = Integer.parseInt(sc.next());
            LinkedList<Integer> list = new LinkedList<>();
            int head = Integer.parseInt(sc.next());
            list.add(head);
            for (int i = 1; i < n; i++) {
                int pre = Integer.parseInt(sc.next());
                int cur = Integer.parseInt(sc.next());
                int indexInsert = list.indexOf(cur) + 1;
                list.add(indexInsert, pre);
            }
            int delete = Integer.parseInt(sc.next());
            list.removeIf(integer -> integer.equals(delete));
            if (list.size() == 0) {
                System.out.println("null");
            } else {
                for (Integer integer : list) {
                    System.out.print(integer + " ");
                }
            }
        }
    }
}
全部评论

相关推荐

鼠鼠第一次实习,啥也不懂一直是自己一个人吃的饭,不会做工作老是被嫌弃,大人的世界是这样的吗?
我是星星我会发亮:好的mt有两种,一种愿意教你的,一种几乎什么活都不给你派让你很闲允许你做自己事情的
实习吐槽大会
点赞 评论 收藏
分享
点赞 评论 收藏
分享
_mos_:我以为手抄报简历就已经很顶了,没想到还有表格简历
点赞 评论 收藏
分享
评论
点赞
1
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务