Nowcoder-HJ48从单向列表中删除指定节点的值

1 篇文章 0 订阅
1 篇文章 0 订阅

Nowcoder-HJ48从单向列表中删除指定节点的值

万万没想到这么常规的题目,又学到了新的东西:构造链表
因为牛客网的输入输出需要自己写,在链表没有搭建好的情况下,如何删除一个节点的前提是要有一个完整的链表。

思路

常规的从单向列表中删除指定节点的值伪代码:

int deletNum ;
ListNode cur = head;
while(cur != null){
	if(cur.next.val == deletNum){
		cur.next = cur.next.next;
		break;
	}else{
	cur = cur.next;
	}
}

那么如何自己构造链表呢?

先来看看题目:(题目来源于牛客网华为机试)
在这里插入图片描述

因为输入是 int 型,所以构建链表就有了点难度。

int number = sc.nextInt(); // number 是链表长度
for (int i = 0; i < number; i++) { 
   if (i == 0) { // 此时加入的是第一个节点,即头结点,也就是输入的第2行
       head = new ListNode(sc.nextInt(), null);
       cur = head;
   } else {
       cur = head;
       int a = sc.nextInt(); // 例如,从输入的第三行开始a = 3
       int b = sc.nextInt();//b = 2
       while (cur != null) { // 找b
           if (cur.next == null) {// cur.val = b,并且cur.next = null (找到最后)
               cur.next = new ListNode(a, null);
               break;
           } else if (cur.val == b) {// cur.val = b,但cur此时不是链表的最后一个节点
               cur.next = new ListNode(a, cur.next);
               break;
           } else { //cur.val != b  继续往后找
               cur = cur.next;
           }
       }
   }
}

完整代码如下:

import java.util.Scanner;

public class RemoveListNodeVal {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        while (sc.hasNext()) {
            int number = sc.nextInt();
            ListNode head = null;
            ListNode cur = null;
            for (int i = 0; i < number; i++) {
                if (i == 0) {
                    head = new ListNode(sc.nextInt(), null);
                    cur = head;
                } else {
                    cur = head;
                    int a = sc.nextInt();
                    int b = sc.nextInt();
                    while (cur != null) {
                        if (cur.next == null) {
                            cur.next = new ListNode(a, null);
                            break;
                        } else if (cur.val == b) {
                            cur.next = new ListNode(a, cur.next);
                            break;
                        } else {
                            cur = cur.next;
                        }
                    }
                }
            }
            int deletNum = sc.nextInt();
            boolean falg = false;
            cur = head;
            if (head.val == deletNum) {
                head = head.next;
                falg = true;
            } else {
                while (cur.next != null) {
                    if (cur.next.val == deletNum) {
                        cur.next = cur.next.next;
                        falg = true;
                        break;
                    }
                    cur = cur.next;
                }
            }
            if (falg) {
                cur = head;
                while (cur != null) {
                    System.out.print(cur.val + " ");
                    cur = cur.next;
                }
                System.out.println();
            }
        }
    }

    static class ListNode {
        int val;
        ListNode next;
        public ListNode(int val, ListNode next) {
            this.val = val;
            this.next = next;
        }
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值