【发布时间】:2016-10-25 19:05:26
【问题描述】:
我有一个项目问题,我需要检查一个链表是否是回文,向前和向后一样。我获得了一个名为 Node 类的课程,其中包含我不允许更改的特定信息,因此我必须使用我在该课程中拥有的内容并且不能在那里写更多内容来帮助我。我的问题是我可以通过列表,但我不知道如何倒退并比较值。
免责声明:我不想要一个完整的答案,只是提示/想法!
这是我无法更改且必须使用的课程:
class Node {
/**
* The value stored in the node.
*/
public Character value;
/**
* A pointer to the next node in
* the list.
*/
public Node next;
/**
* A pointer to the previous node
* in the list. (Note: having this
* pointer makes this a "doubly linked"
* list.
*/
public Node prev;
/**
* A constructor to set up a
* node with a value.
*
* @param value the value to put in the node
*/
public Node(Character value) {
this.value = value;
}
/**
* A constructor to set up a
* node with a value and a pointer
* to the previous node (so I can
* append items easily in my test
* code).
*
* @param value the value to put in the node
* @param prev the previous node in the list
*/
public Node(Character value, Node prev) {
this.value = value;
this.prev = prev;
}
/**
* Sets the next node in the list.
*
* @param n the next node in the list
*/
public void setNext(Node n) {
next = n;
}
/**
* Sets the previous node in the list.
*
* @param n the previous node in the list
*/
public void setPrev(Node n) {
prev = n;
}
}
到目前为止,这就是我所拥有的,我只是尽我所能:
public static boolean listPalindrome(Node input) {
if(input == null)
{
throw new IllegalArgumentException();
}
int count = 0;
while(input.next != null)
{
count++;
}
Node holder = new Node(null, input.next);
for(int i = 0;i<count;i++)
{
if(input.next != null)
{
break;
}
else
{
}
}
return false; //replace this
}
请帮忙! 谢谢!
【问题讨论】:
-
我正在检查它,但它并没有给我一个我需要的想法。
标签: java