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

Hackerrank Print all Nodes链表题Java实现报错求解

问题说明

实现链表元素打印的题目时代码编译不通过,共报7个错误,核心疑问是如何正确为链表设置head头节点。

原错误实现代码:

import java.io.*;
import java.util.*;

public class Solution {
    
    private class Node{
        int data;
        Node next;
        private Node (int data){
        this.data = data;
        this.next = null;
        }
    }
    
    static void setHead(LinkedList<Integer> lList){
        Node head;
        if (lList.peek() != null){
            head = lList.peek();
            return;
        }else{
            return;
        }
    }
    
    static void input(LinkedList<Integer> lList){
        Scanner scanner = new Scanner(System.in);
        int count = scanner.nextInt();
        int i = 0;
        while (i < count){
            int data = scanner.nextInt();
            lList.add(data);
            i++;
            }
        scanner.close();
        return;
    }
    static void printLinkedList(LinkedList<Integer> lList){
        while(lList.head != null){
            Sdtout.println(lList.head.data);
            this.head = head.next;
        }
        return;
    }
    

    public static void main(String[] args) {
        LinkedList<Integer> n1 = new LinkedList<Integer>();
        setHead(n1);
        input(n1);
        printLinkedList(n1);
    }
}

编译报错信息:

Solution.java:19: error: incompatible types: Integer cannot be converted to Solution.Node
            head = lList.peek();
                             ^
Solution.java:39: error: cannot find symbol
        while(lList.head != null){
                   ^
  symbol:   variable head
  location: variable lList of type LinkedList<Integer>
Solution.java:40: error: cannot find symbol
            Sdtout.println(lList.head.data);
                                ^
  symbol:   variable head
  location: variable lList of type LinkedList<Integer>
Solution.java:40: error: cannot find symbol
            Sdtout.println(lList.head.data);
            ^
  symbol:   variable Sdtout
  location: class Solution
Solution.java:41: error: non-static variable this cannot be referenced from a static context
            this.head = head.next;
            ^
Solution.java:41: error: cannot find symbol
            this.head = head.next;
                ^
  symbol: variable head
Solution.java:41: error: cannot find symbol
            this.head = head.next;
                        ^
  symbol:   variable head
  location: class Solution
7 errors
错误根因
  • 核心实现方向错误:题目考察自定义链表的遍历逻辑,不需要使用JDK内置的java.util.LinkedList,自定义的Node类型和内置LinkedList存储的Integer类型完全不兼容,直接赋值必然触发类型转换错误。
  • 非法访问类非公开属性:JDK内置的LinkedList没有对外暴露head成员变量,外部代码直接访问lList.head会触发找不到符号的错误。
  • 低级笔误:标准输出的正确写法是System.out,错写为Sdtout。
  • 静态方法语法错误:printLinkedList是静态方法,不能用this关键字引用实例属性,且类中根本没有定义名为head的成员变量,多处引用head都会触发报错。
  • 无效逻辑:setHead方法里的head是方法局部变量,方法执行结束就会被销毁,完全不会修改传入的链表对象,达不到设置头节点的效果。
正确实现思路

这道题不需要从零写输入处理、链表构建的逻辑,OJ平台的代码模板已经把这些逻辑预置完成,只需要实现核心的遍历打印函数即可,逻辑非常简单:

  1. 函数接收链表头节点作为入参,定义一个临时游标变量指向头节点,避免修改原头节点的指向
  2. 循环判断当前游标节点是否为空:不为空就打印当前节点的data值,然后把游标移动到当前节点的next位置
  3. 游标为空时说明已经遍历完整个链表,直接结束即可

正确可通过的完整代码:

import java.io.*;
import java.util.*;

public class Solution {

    // 题目预置的节点类
    static class SinglyLinkedListNode {
        public int data;
        public SinglyLinkedListNode next;
        public SinglyLinkedListNode(int nodeData) {
            this.data = nodeData;
            this.next = null;
        }
    }

    // 题目预置的链表类
    static class SinglyLinkedList {
        public SinglyLinkedListNode head;
        public SinglyLinkedListNode tail;
        public SinglyLinkedList() {
            this.head = null;
            this.tail = null;
        }
        public void insertNode(int nodeData) {
            SinglyLinkedListNode node = new SinglyLinkedListNode(nodeData);
            if (this.head == null) {
                this.head = node;
            } else {
                this.tail.next = node;
            }
            this.tail = node;
        }
    }

    // 仅需要补全这部分核心遍历逻辑
    static void printLinkedList(SinglyLinkedListNode head) {
        SinglyLinkedListNode cur = head;
        while (cur != null) {
            System.out.println(cur.data);
            cur = cur.next;
        }
    }

    // 题目预置的输入处理逻辑
    private static final Scanner scanner = new Scanner(System.in);
    public static void main(String[] args) {
        SinglyLinkedList llist = new SinglyLinkedList();
        int llistCount = scanner.nextInt();
        scanner.skip("(\r\n|[\n\r\u2028\u2029\u0085])?");
        for (int i = 0; i < llistCount; i++) {
            int llistItem = scanner.nextInt();
            scanner.skip("(\r\n|[\n\r\u2028\u2029\u0085])?");
            llist.insertNode(llistItem);
        }
        printLinkedList(llist.head);
        scanner.close();
    }
}
注意事项
  • 做在线判题平台的算法题时,先仔细看题目给出的初始代码模板,不要直接删掉所有预置代码自己重写,大部分时候输入输出、基础数据结构定义都已经写好,只需要补全核心逻辑即可。
  • 除非题目明确要求使用内置类,否则实现链表、树等自定义数据结构的题目时,不要混用JDK内置的集合类,两者的API、内部结构完全不通用。
  • 注意变量作用域:方法内定义的局部变量仅在当前方法内有效,静态方法不能直接访问非静态的实例成员。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.30 15:42:23