xiao_zha_ 2024-09-24 20:11 采纳率: 0%
浏览 3

力扣报错这个问题该怎么解决

问题

Line 7: error: incompatible types: ListNode cannot be converted to MyList [in Driver.java]
ListNode ret = new Solution().addTwoNumbers(param_1, param_2);
^

在力扣提交遇到这个问题,但是编译器本身没报错


import java.util.Scanner;

class ListNode {
    public int val = 0;
    public ListNode next;

    public int length() {
        int i = 0;
        ListNode current = this;
        while (current != null) {
            i++;
            current = current.next;
        }
        return i;
    }
}

class MyList {
    public ListNode header;

    public void addLast(int data){
        ListNode node = new ListNode();
        node.val=data;
        node.next=null;
        if(this.header == null){
            this.header = node;
        }else {
            ListNode cur = this.header;
            while(cur.next != null){
                cur = cur.next;
            }
            cur.next = node;
        }
    }


}

class Solution {
    public MyList addTwoNumbers(MyList list1, MyList list2) {
        MyList list3 = new MyList();
        int m = 0;
        int n = 0;
        ListNode current1 = list1.header;
        ListNode current2 = list2.header;
        while (current1 != null || current2 != null || n > 0) {
            int val1 = (current1 != null) ? current1.val : 0;
            int val2 = (current2 != null) ? current2.val : 0;
            m = (val1 + val2 + n) % 10;
            n = (val1 + val2 + n) / 10;
            list3.addLast(m);
            if (current1 != null) current1 = current1.next;
            if (current2 != null) current2 = current2.next;
        }
        return list3;
    }
}

class Out {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int m, n;
        m = scanner.nextInt();
        n = scanner.nextInt();
        MyList list1 = new MyList();
        MyList list2 = new MyList();
        for (int i = 0; i < m; i++) {
            list1.addLast(scanner.nextInt());
        }
        for (int i = 0; i < n; i++) {
            list2.addLast(scanner.nextInt());
        }
        Solution s = new Solution();
        MyList list3 = s.addTwoNumbers(list1, list2);
        ListNode current = list3.header;
        while (current != null) {
            System.out.print(current.val);
            current = current.next;
        }
        scanner.close();
    }
}






  • 写回答

1条回答 默认 最新

  • 一轮明月照丘壑 2024-09-24 20:12
    关注

    以下回复参考:皆我百晓生券券喵儿等免费微信小程序作答:

    根据你提供的代码和错误信息,错误发生在你试图将 ListNode 类型对象赋值给 MyList 类型变量。在 Java 中,不同的数据类型是不兼容的,即使它们有相似的属性或行为。这是因为在编译时期,编译器需要确保数据类型的正确性以保证程序的稳定运行。在这种情况下,你的函数 addTwoNumbers 返回类型是 MyList,但是在你调用它时,你试图将其返回值赋给一个 ListNode 类型的变量 ret,这是不允许的。为了解决这个问题,你需要更改返回类型或更改变量类型。

    如果你想要返回一个新的链表,那么你应该让 addTwoNumbers 函数返回 ListNode 类型而不是 MyList 类型。你的函数可以修改如下:

    public ListNode addTwoNumbers(ListNode list1, ListNode list2) {
        // ...(其他代码不变)
    }
    

    然后在你的 main 函数中,你可以直接接收 ListNode 类型作为返回值:

    Solution s = new Solution();
    ListNode list3 = s.addTwoNumbers(list1, list2);
    

    然后你可以按照你的需求处理这个返回的链表。如果你需要遍历这个链表并打印所有节点的值,你可以这样做:

    ListNode current = list3;
    while (current != null) {
        System.out.print(current.val);
        current = current.next;
    }
    

    另外注意一点是,如果你的输入是数字数组,你需要一种方式来将数组转换为链表。在当前的代码中,你直接通过 MyListaddLast 方法添加了数字,但是这并没有将数字添加到链表中。你需要创建相应的节点并将其添加到链表中。你可能需要修改你的输入处理和链表构建部分的代码以适应这种情况。

    评论

报告相同问题?

问题事件

  • 创建了问题 9月24日

悬赏问题

  • ¥15 如何在vue.config.js中读取到public文件夹下window.APP_CONFIG.API_BASE_URL的值
  • ¥50 浦育平台scratch图形化编程
  • ¥20 求这个的原理图 只要原理图
  • ¥15 vue2项目中,如何配置环境,可以在打完包之后修改请求的服务器地址
  • ¥20 微信的店铺小程序如何修改背景图
  • ¥15 UE5.1局部变量对蓝图不可见
  • ¥15 一共有五道问题关于整数幂的运算还有房间号码 还有网络密码的解答?(语言-python)
  • ¥20 sentry如何捕获上传Android ndk 崩溃
  • ¥15 在做logistic回归模型限制性立方条图时候,不能出完整图的困难
  • ¥15 G0系列单片机HAL库中景园gc9307液晶驱动芯片无法使用硬件SPI+DMA驱动,如何解决?