题解 | 【模板】链表

【模板】链表

https://www.nowcoder.com/practice/97dc1ac2311046618fd19960041e3c6f

import java.util.Scanner;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {

    static class ListNode {
        int val;
        ListNode next;

        ListNode(int x) {
            this.val =x;
        }

    }

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        // 注意 hasNext 和 hasNextLine 的区别

        int n = in.nextInt(); // 操作的次数
        ListNode dummy = new ListNode(-1);
        in.nextLine();
        while (n-- > 0) {
            String line = in.nextLine();
            String[] arr = line.split(" ", -1);
            
            if (arr[0].equals("insert")) {
               
                insert(dummy, arr[1], arr[2]);
            }else if (arr[0].equals("delete")) {
                
                delete(dummy, arr[1]);
            }
        }
        print(dummy);
    }


    private static void insert(ListNode dummy, String xStr, String yStr) {
        int x = Integer.parseInt(xStr);
        int y = Integer.parseInt(yStr);
        ListNode cur = dummy;
        while (cur != null && cur.next != null) {
            if (cur.next.val == x) {
                //    插入
                ListNode node = new ListNode(y);
                node.next = cur.next;
                cur.next = node;
                return;
            }
            cur = cur.next;
        }
        ListNode node = new ListNode(y);
        cur.next = node;
    }

    private static void delete(ListNode dummy, String xStr) {
        int x = Integer.parseInt(xStr);
        ListNode cur = dummy;
        while(cur != null && cur.next != null) {
            if (cur.next.val == x) {
                cur.next = cur.next.next;
                return;
            }   
            cur = cur.next;  
        }
    }



    private static void print(ListNode head) {
        if (head.next == null) {
            System.out.println("NULL");
            return;
        }
        ListNode cur = head.next;

        while (cur != null) {
            System.out.print(cur.val + " ");
            cur = cur.next;
        }
        
    }
}

全部评论

相关推荐

01-30 22:03
门头沟学院 Java
用微笑面对困难:我滴妈,【俩月】【实习】【主管】仨debuff吃满了,独立设计开发的项目写了绝大占比的运营板块,你独立开发,那维护、问题复盘、日志更新、bug、策划书全是自己整的? 不建议写那么大,可以从小出发更容易
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
正在热议
更多
# 春招至今,你的战绩如何? #
9396次浏览 87人参与
# 你的实习产出是真实的还是包装的? #
1708次浏览 40人参与
# 米连集团26产品管培生项目 #
5700次浏览 214人参与
# 军工所铁饭碗 vs 互联网高薪资,你会选谁 #
7436次浏览 43人参与
# 简历第一个项目做什么 #
31541次浏览 329人参与
# 重来一次,我还会选择这个专业吗 #
433336次浏览 3926人参与
# MiniMax求职进展汇总 #
23809次浏览 308人参与
# 当下环境,你会继续卷互联网,还是看其他行业机会 #
186951次浏览 1120人参与
# 牛客AI文生图 #
21408次浏览 238人参与
# 不考虑薪资和职业,你最想做什么工作呢? #
152285次浏览 887人参与
# 研究所笔面经互助 #
118864次浏览 577人参与
# 简历中的项目经历要怎么写? #
310032次浏览 4191人参与
# AI时代,哪些岗位最容易被淘汰 #
63371次浏览 801人参与
# 面试紧张时你会有什么表现? #
30484次浏览 188人参与
# 你今年的平均薪资是多少? #
213002次浏览 1039人参与
# 你怎么看待AI面试 #
179831次浏览 1232人参与
# 高学历就一定能找到好工作吗? #
64304次浏览 620人参与
# 你最满意的offer薪资是哪家公司? #
76430次浏览 374人参与
# 我的求职精神状态 #
447977次浏览 3128人参与
# 正在春招的你,也参与了去年秋招吗? #
363224次浏览 2637人参与
# 腾讯音乐求职进展汇总 #
160574次浏览 1110人参与
# 校招笔试 #
470312次浏览 2962人参与
牛客网
牛客网在线编程
牛客网题解
牛客企业服务