我正在尝试使用队列检查链表是否是回文。如果链表是回文,则solve()函数返回true。即使值相等,Equatingq.peek与 Node 值也会返回false。尝试打印q.peek()返回LList$Node@7852e922。我做了谷歌它说像队列节点值在以前的功能调用中使用,没有得到太多。public class LList { private Node head = null; static class Node { int data; Node next; Node(int d) { data = d; next = null; } } public void push(int d) { Node n = new Node(d); n.next = head; head = n; } public boolean solve(Node t, Queue q) { if (t == null) { return true; } q.add(t.data); if (solve(t.next, q)) { **System.out.println(q.peek());**//LList$Node@7852e922 if (q.peek().equals(t.data)) { q.remove(); } else { return false; } } else { return false; } return true; } public static void main(String args[]) { LList lList = new LList(); lList.push(5); lList.push(4); lList.push(3); lList.push(4); lList.push(5); Queue<Integer> q = new LinkedList<Integer>(); System.out.println(lList.solve(lList.head, q)); }}
2 回答
慕码人8056858
TA贡献1803条经验 获得超6个赞
您将一个节点添加到此处的队列中: q.dd(t)
在这种方法中:
public boolean solve(Node t, Queue q) {
if (t == null) {
return true;
}
if (q == null) {
q = new LinkedList<Integer>();
}
q.add(t);
if (solve(t.next, q)) {
**System.out.println(q.peek());**//LList$Node@7852e922
if (q.peek().equals(t.data)) {
q.remove();
} else {
return false;
}
} else
return false;
return true;
}
你是故意的q.add(t.data)吗?
添加回答
举报
0/150
提交
取消