帮我找出代码的错误, public static int[] reversePrint(ListNode head) { ListNode current = head; LinkedList<Integer> ll = new LinkedList<>(); where(current.next != null){ ll.addLast(current.val); current = current.next; } int[] a = ll.toArray(); return a; }
时间: 2024-02-14 19:26:34 浏览: 19
你的代码存在一些错误。首先,在 `where` 关键字后面应该是一个条件语句,而不是一个表达式。此外,你需要将链表中的元素逆序添加到 `LinkedList` 中,而不是原样添加。最后,`ll.toArray()` 返回的是一个 `Object[]` 数组,需要使用 `Arrays.copyOf()` 将其转换为 `int[]` 数组。以下是修正后的代码:
```java
import java.util.LinkedList;
import java.util.Arrays;
public static int[] reversePrint(ListNode head) {
ListNode current = head;
LinkedList<Integer> ll = new LinkedList<>();
while (current != null) {
ll.addFirst(current.val);
current = current.next;
}
int[] a = new int[ll.size()];
for (int i = 0; i < ll.size(); i++) {
a[i] = ll.get(i);
}
return a;
}
```
这样,你就可以将链表中的元素逆序存储在 `int` 数组中并返回了。
相关问题
ListNode* buildList(int* arr,int n) { ListNode *head=new ListNode,*val,*cul=*head; for(int j=0;j<n;j++){ val=new ListNode; cul->next=val; cul->ListNode(*(arr+i)); cul=val; } return head; } ListNode* reverse(head) { ListNode* hh,*left=head,*right; hh=head->next; right=hh->next; hh->next=NULL; while(right!=NULL){ left=hh; hh=right; right=rigth->next; hh->next=left; } return hh; } void printList(head) { while(head!=NULL){ cout<<head->val<<' '; head=head->next; } } 这段代码错在哪里,怎么改
这段代码有以下几个问题:
1. 在 `buildList` 函数中,第一个节点没有被初始化,即 `head->val` 没有被赋值。应该将 `head` 初始化为 `NULL`,并在循环内部创建新节点并赋值。
2. 在 `buildList` 函数中,第 8 行的 `cul->ListNode(*(arr+i))` 是无意义的,应该将其改为 `cul->val = *(arr + j)`。
3. 在 `reverse` 函数中,第 2 行应该指定 `head` 的类型为 `ListNode*`,即 `ListNode* reverse(ListNode* head)`。
4. 在 `reverse` 函数中,第 4 行的 `right=rigth->next` 中的 `rigth` 拼写错误,应该改为 `right=right->next`。
下面是修改后的代码:
```
ListNode* buildList(int* arr, int n) {
ListNode* head = NULL;
ListNode *val, *cul = head;
for (int j = 0; j < n; j++) {
val = new ListNode;
val->val = *(arr + j);
if (cul == NULL) {
head = val;
cul = val;
} else {
cul->next = val;
cul = val;
}
}
return head;
}
ListNode* reverse(ListNode* head) {
ListNode *hh, *left = head, *right;
hh = head->next;
right = hh->next;
hh->next = NULL;
while (right != NULL) {
left = hh;
hh = right;
right = right->next;
hh->next = left;
}
return hh;
}
void printList(ListNode* head) {
while (head != NULL) {
cout << head->val << ' ';
head = head->next;
}
}
```
#include<iostream> using namespace std; /* * Definition of ListNode */ class ListNode { public: int val; ListNode *next; ListNode(int val) { this->val = val; this->next = NULL; } }; class Solution { public: ListNode* buildList(int* arr,int n) { ListNode *head=new ListNode,*val,*cul=*head; for(int j=0;j<n;j++){ val=new ListNode; cul->next=val; cul->ListNode(*(arr+i)); cul=val; } return head; } ListNode* reverse(head) { ListNode* hh,*left=head,*right; hh=head->next; right=hh->next; hh->next=NULL; while(right!=NULL){ left=hh; hh=right; right=right->next; hh->next=left; } return hh; } void printList(head) { while(head!=NULL){ cout<<head->val<<' '; head=head->next; } } }; int main() { int n; cin>>n; int* arr = new int[n]; for(int i=0;i<n;i++) cin>>arr[i]; Solution obj; ListNode* head = obj.buildList(arr,n); head = obj.reverse(head); obj.printList(head); delete[] arr; return 0; }这段代码错哪了,怎么改
这段代码有以下几个问题:
1. 在 `buildList` 函数中,第一行的 `*head` 是一个未初始化的指针,会导致程序崩溃。应该将其改为 `head = new ListNode`,即创建一个新的节点并将其地址赋给 `head`。
2. 在 `buildList` 函数中,第 5 行的 `cul=*head` 是错误的,应该将其改为 `cul=head`,即将 `cul` 初始化为 `head`。
3. 在 `reverse` 函数中,第 2 行应该指定 `head` 的类型为 `ListNode*`,即 `ListNode* reverse(ListNode* head)`。
4. 在 `reverse` 函数中,第 4 行的 `right=rigth->next` 中的 `rigth` 拼写错误,应该改为 `right=right->next`。
5. 在 `printList` 函数中,第一行应该指定 `head` 的类型为 `ListNode*`,即 `void printList(ListNode* head)`。
下面是修改后的代码:
```
#include <iostream>
using namespace std;
/* Definition of ListNode */
class ListNode {
public:
int val;
ListNode* next;
ListNode(int val) {
this->val = val;
this->next = NULL;
}
};
class Solution {
public:
ListNode* buildList(int* arr, int n) {
ListNode* head = new ListNode(0);
ListNode* val, * cul = head;
for (int j = 0; j < n; j++) {
val = new ListNode(*(arr + j));
cul->next = val;
cul = val;
}
return head->next;
}
ListNode* reverse(ListNode* head) {
ListNode* hh, * left = head, * right;
hh = head->next;
right = hh->next;
hh->next = NULL;
while (right != NULL) {
left = hh;
hh = right;
right = right->next;
hh->next = left;
}
return hh;
}
void printList(ListNode* head) {
while (head != NULL) {
cout << head->val << ' ';
head = head->next;
}
}
};
int main() {
int n;
cin >> n;
int* arr = new int[n];
for (int i = 0; i < n; i++)
cin >> arr[i];
Solution obj;
ListNode* head = obj.buildList(arr, n);
head = obj.reverse(head);
obj.printList(head);
delete[] arr;
return 0;
}
```