题解 | 【模板】链表

【模板】链表

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;
        }
        
    }
}

全部评论

相关推荐

牛客51274894...:照片认真的吗,找个专门拍证件照的几十块钱整端正点吧,要不就别加照片
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
正在热议
更多
# 春招至今,你的战绩如何? #
8542次浏览 77人参与
# 你的实习产出是真实的还是包装的? #
1566次浏览 40人参与
# 米连集团26产品管培生项目 #
5484次浏览 214人参与
# 军工所铁饭碗 vs 互联网高薪资,你会选谁 #
7320次浏览 40人参与
# 简历第一个项目做什么 #
31468次浏览 323人参与
# 当下环境,你会继续卷互联网,还是看其他行业机会 #
186755次浏览 1118人参与
# 巨人网络春招 #
11282次浏览 223人参与
# 不考虑薪资和职业,你最想做什么工作呢? #
152222次浏览 887人参与
# 研究所笔面经互助 #
118833次浏览 577人参与
# 重来一次,我还会选择这个专业吗 #
433251次浏览 3926人参与
# 简历中的项目经历要怎么写? #
309889次浏览 4181人参与
# 面试紧张时你会有什么表现? #
30463次浏览 188人参与
# 你今年的平均薪资是多少? #
212941次浏览 1039人参与
# AI时代,哪些岗位最容易被淘汰 #
63209次浏览 791人参与
# 我的求职精神状态 #
447934次浏览 3128人参与
# 你最满意的offer薪资是哪家公司? #
76375次浏览 374人参与
# 正在春招的你,也参与了去年秋招吗? #
363077次浏览 2635人参与
# 你怎么看待AI面试 #
179724次浏览 1223人参与
# 牛客AI文生图 #
21391次浏览 237人参与
# 职能管理面试记录 #
10776次浏览 59人参与
# 网易游戏笔试 #
6445次浏览 83人参与
# 腾讯音乐求职进展汇总 #
160536次浏览 1109人参与
牛客网
牛客网在线编程
牛客网题解
牛客企业服务