问题
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();
}
}