You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

为何checkAB函数无法正常工作?Java链表代码问题排查

问题分析与修复:检查链表中相邻的'a'和'b'

先明确你的需求:你希望checkAB函数能判断链表中是否存在两种情况之一——要么某个'a'的下一个节点是'b',要么某个'b'的下一个节点是'a'。但当前的实现存在几个关键问题,导致它无法正常工作,咱们来一步步拆解并修复:

现存问题点

  • 空指针风险:如果链表中不存在'a'或者'b',sod1函数会返回null,这时候调用getNext()直接会抛出NullPointerException,程序直接崩溃。
  • 类型比较错误:getNext()返回的是Node<Character>对象,你直接用它和字符'b'/'a'做equals()比较,这是在比较对象和原始字符,类型不匹配,永远不会返回true。你需要先获取节点的value再做比较。
  • 逻辑不完整:就算找到了'a',你只检查了第一个'a'的下一个节点,没遍历整个链表找所有可能的相邻情况;同理'b'也是一样。

修复方案

我们可以调整checkAB的实现,直接遍历链表,逐个检查当前节点和下一个节点的组合是否符合要求,这样更高效也更安全:

修复后的完整代码

public class MainClass { 
    public static void main(String[] args) { 
        Node<Character> n1 = new Node<Character>('b'); 
        Node<Character> n2 = new Node<Character>('a'); 
        Node<Character> n3 = new Node<Character>('s'); 
        Node<Character> n4 = new Node<Character>('h'); 
        n1.setNext(n2); 
        n2.setNext(n3); 
        n3.setNext(n4); 
        System.out.println(checkAB(n1)); // 这里会返回true,因为b的下一个是a
    }

    public static Node<Character> sod1(Node<Character> lst, char ch){ 
        if(lst == null) return null; 
        if(lst.getValue() == ch) return lst; 
        return sod1(lst.getNext(), ch); 
    } 

    public static boolean checkAB(Node<Character> lst) { 
        // 遍历链表,确保当前节点和下一个节点都不为空
        while (lst != null && lst.getNext() != null) {
            char currentChar = lst.getValue();
            char nextChar = lst.getNext().getValue();
            // 检查当前节点与下一个节点是否是目标组合
            if ((currentChar == 'a' && nextChar == 'b') || (currentChar == 'b' && nextChar == 'a')) {
                return true;
            }
            // 移动到下一个节点继续检查
            lst = lst.getNext();
        }
        // 遍历完所有节点都没找到符合条件的组合
        return false;
    } 
}

// 补充Node类实现(确保代码可运行)
class Node<T> {
    private T value;
    private Node<T> next;

    public Node(T value) {
        this.value = value;
    }

    public T getValue() {
        return value;
    }

    public Node<T> getNext() {
        return next;
    }

    public void setNext(Node<T> next) {
        this.next = next;
    }
}

修复说明

  1. 遍历链表替代单次查找:直接遍历每个节点,检查当前节点和下一个节点的字符组合,能覆盖链表中所有可能的相邻情况,而不是只检查第一个'a'或'b'的后续节点。
  2. 避免空指针异常:循环条件先判断lst和lst.getNext()是否为空,确保不会在空节点上调用方法。
  3. 正确的字符比较:先获取节点的value,再用==直接比较字符(字符是原始类型,==可直接比较值)。

如果你坚持想用原有的sod1函数修改,也可以做如下调整(但这种方式只能检查第一个'a'和第一个'b'的后续节点,无法覆盖其他位置的组合):

public static boolean checkAB(Node<Character> lst) { 
    Node<Character> aNode = sod1(lst, 'a');
    // 先判断节点是否存在且有后续节点,再比较值
    boolean aFollowedByB = (aNode != null && aNode.getNext() != null && aNode.getNext().getValue() == 'b');
    
    Node<Character> bNode = sod1(lst, 'b');
    boolean bFollowedByA = (bNode != null && bNode.getNext() != null && bNode.getNext().getValue() == 'a');
    
    return aFollowedByB || bFollowedByA;
}

内容的提问来源于stack exchange,提问作者Dan _

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.08 20:17:36