我有以下代码,node.val中的"ListNode"one_answers"val"都是红色的,表示它没有注册它们。此外,未注册"Node"类型。我认为Node类似于"int"、"double"等等。
class Solution {
public void deleteNode(ListNode node) {
while(node.next != null) {
int temp = node.val;
node.val = node.next.val;
node.next.val = temp;
if(node.next.next == null) {
node.next = null;
break;
}
node = node.next;
}
}
public static void main(String[] args) {
ListNode val = [4,5,1,9],;
Solution obj = new Solution();
val = obj.deleteNode(val);
for (Node node = val; node != null; node = node.next) {
System.out.println(node.data);
}
}
}
class ListNode {
int val;
ListNode next = null;
public ListNode(int d) {
val = d;
ListNode next = null;
}
ListNode appendToTail(int d) {
ListNode end = new ListNode(d);
ListNode n = this;
while (n.next != null) {
n = n.next;
}
n.next = end;
return end;
}
public void deleteNode(ListNode node) {
while(node.next != null) {
int temp = node.val;
node.val = node.next.val;
node.next.val = temp;
if(node.next.next == null) {
node.next = null;
break;
}
node = node.next;
}
}
public static void main(String[] args) {
ListNode val = new ListNode(4);
val.appendToTail(5);
val.appendToTail(1);
val.appendToTail(9);
val.deleteNode(val.next.next);
for (ListNode node = val; node != null; node = node.next) {
System.out.println(node.val);
}
}
}