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

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

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

涉及链表的操作可以用LinkedList,其底层就是用链表实现的。

public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);

        while (in.hasNextInt()) { // 注意 while 处理多个 case
            int n = in.nextInt();
            int head = in.nextInt();
            List<Integer> list = new LinkedList<>();
            list.add(head);
            for(int i =0;i<n-1;++i){
                int insertnum = in.nextInt();
                int pre = in.nextInt();
                for(int j = 0;j<list.size();++j){
                    if(list.get(j)==pre){
                        list.add(j+1,insertnum);
                        break;
                    }
                    
                }
            }
            int deletenum = in.nextInt();
            for(Integer i:list){
                if(i!=deletenum)
                    System.out.print(i+" ");
            }
        }
    }
**}**
全部评论

相关推荐

2025-12-11 14:24
门头沟学院 Java
牛客35720396...:不要用boss,全是骗
点赞 评论 收藏
分享
评论
1
2
分享

创作者周榜

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