我需要使用递归编写一个 contains 方法,这意味着查找某个节点中是否存在“元素”。
public class SortedSetNode implements Set
{
protected String value;
protected SortedSetNode next;
}
public boolean contains(String el) {
if (next.getValue().equals(el))
{
return true;
}
else
{
next.contains(el);
}
}
最佳答案
public boolean contains(String el) {
if (value.equals(el)) return true;
if (next == null) return false;
else return next.contains(el);
}
关于java - 如何使用递归编写链表的 contains 方法? java ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18928379/