我正在尝试使用队列检查链表是否是回文。如果链表是回文,则
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));
}
}
慕码人8056858
相关分类