【发布时间】:2015-05-28 14:11:40
【问题描述】:
我是 Java 编码的新手,希望你们能提供帮助:)
我想要达到的目标。
A->B->C->...
一个
A->B->C->...
B->A
A->B->C->...
C->B->A
这是我的代码,希望你们能够帮助我找出为什么我总是得到一个空列表。
public StringList reverse(){
Node cursor = head;
String temp;
while(cursor!=null){
temp = cursor.getElement();
head = new Node(temp,head);
cursor = cursor.getNext();
}
return new StringList();}
感谢您的回复和答案设法让它工作:) 这里的代码供其他人将来参考。
公共字符串列表反向(){ StringList newList = new StringList();
Node cursor = head;
String temp;
while(cursor!=null){
temp = cursor.getElement();
newList.head=new Node(temp,newList.head);
cursor = cursor.getNext();
}
return newList;}
【问题讨论】:
-
你在函数的末尾返回了一个新的 StringList。
-
缺少很多代码吗?
head来自哪里?你对新的head什么都不做,当然应该以某种方式返回。 -
谢谢您,先生,但是我怎样才能将新节点添加到新的字符串列表中?如果我删除新的 StringList,返回值是多少?