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

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

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

import java.util.*;
import java.lang.*;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        // 注意 hasNext 和 hasNextLine 的区别
        while (in.hasNext()) { // 注意 while 处理多个 case
            int num = in.nextInt();
            int first = in.nextInt();
            LinkedList<Integer> ll = new LinkedList<>();

            int m0 = in.nextInt();
            int m1 = in.nextInt();
            ll.add(m1);
            ll.add(m0);
            for (int i = 2; i < num; i++) {
                int n0 = in.nextInt();
                int n1 = in.nextInt();
                int ind = ll.indexOf(n1);
                if (ind != -1) {
                    ListIterator<Integer> li = ll.listIterator(ind);
                    li.next();
                    li.add(n0);
                } else if (ind == -1) {
                    ll.add(n1);
                    ll.add(n0);
                }
            }
            int del = in.nextInt();
            ll.remove(ll.indexOf(del));
            ll.stream().forEach(k->System.out.printf("%d ", k));
        }
    }
}

全部评论

相关推荐

不愿透露姓名的神秘牛友
07-03 18:22
投了几百份简历,专业和方向完全对口,都已读不回。尝试改了一下学校,果然有奇效。
steelhead:这不是很正常嘛,BOSS好的是即便是你学院本可能都会和聊几句,牛客上学院本机会很少了
点赞 评论 收藏
分享
Lorn的意义:你这种岗位在中国现在要么牛马天天加班,要么关系户进去好吃好喝,8年时间,真的天翻地覆了,对于资本来说你就说一头体力更好的牛马,哎,退伍没有包分配你真的亏了。
点赞 评论 收藏
分享
不愿透露姓名的神秘牛友
今天 11:27
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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