Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java,函数不识别返回语句_Java_Loops_If Statement_Linked List_Return - Fatal编程技术网

Java,函数不识别返回语句

Java,函数不识别返回语句,java,loops,if-statement,linked-list,return,Java,Loops,If Statement,Linked List,Return,在编写检查链表是否为pallindrome的代码时,我创建了一个reverseLL函数,返回一个反向链表和一个isPallindrome函数进行检查。问题是循环中的return语句没有被检测到,只有最后一个返回true的语句;每次都会执行以下操作: 我通过将LL分成两部分并反转下半部分,然后比较两部分来检查LL是否为pallindrome public static Node<Integer> reverseLL(Node<Integer> head){ Node

在编写检查链表是否为pallindrome的代码时,我创建了一个reverseLL函数,返回一个反向链表和一个isPallindrome函数进行检查。问题是循环中的return语句没有被检测到,只有最后一个返回true的语句;每次都会执行以下操作: 我通过将LL分成两部分并反转下半部分,然后比较两部分来检查LL是否为pallindrome

public static Node<Integer> reverseLL(Node<Integer> head){
    Node<Integer> prev = null;
    Node<Integer> current = head;
    Node<Integer> next = null;
    while(current != null) {
        next = current.next;
        current.next = prev;
        prev = current;
        current = next;
    }
    head = prev;
    return head;
}
public static boolean isPallindrome(Node<Integer> head) {
    if(head == null || head.next == null) {
        return true;
    }

    Node<Integer> fast = head;
    Node<Integer> slow = head;

    while(fast.next != null && fast.next.next != null) {
        fast  = fast.next.next;
        slow = slow.next;
    }

    Node<Integer> secondHead = slow.next;
    slow.next = null;
    secondHead = reverseLL(secondHead);

    Node<Integer> p = secondHead;
    Node<Integer> q = head;
    while(p != null) {
        if(p.data != q.data) {
            return false;
        }
        p = p.next;
        q = q.next;
    }
    return true;
}
公共静态节点反向器(节点头){
Node prev=null;
节点电流=头;
Node next=null;
while(当前!=null){
下一个=当前。下一个;
current.next=prev;
prev=当前值;
当前=下一个;
}
头=上一个;
回流头;
}
公共静态布尔值isPalindrome(节点头){
if(head==null | | head.next==null){
返回true;
}
节点快=头;
节点慢=头部;
while(fast.next!=null&&fast.next.next!=null){
fast=fast.next.next;
slow=slow.next;
}
节点secondHead=slow.next;
slow.next=null;
secondHead=反向单元(secondHead);
节点p=第二个头部;
节点q=头部;
while(p!=null){
if(p.data!=q.data){
返回false;
}
p=p.next;
q=q.next;
}
返回true;
}

我不打算运行您的代码,因为它不完整。但是,看起来您找到了列表的最后一个元素,并从那里将其反转,而没有考虑到它也会反转您已经使用
head
引用的列表。因此,当启动比较循环时,有一个指向反向列表中第一个元素的指针和一个指向反向列表中最后一个元素的指针。你绝对不会把清单分成两部分

您的代码也过于复杂。正确的算法是:

find head and tail of list
while (head != tail) {
    if (head.value != tail.value)
        return false;
    head = head.next;
    tail = tail.prev;
}
tail = head
while (tail.next != null) {
    tail = tail.next;
}
您不需要两个循环变量来查找链表的尾部。正确的算法是:

find head and tail of list
while (head != tail) {
    if (head.value != tail.value)
        return false;
    head = head.next;
    tail = tail.prev;
}
tail = head
while (tail.next != null) {
    tail = tail.next;
}
此外,通常不能将整数与相等运算符进行比较。您必须将它们取消装箱到基本整数或使用等于。尝试运行:

System.err.println(new Integer(1) == new Integer(1));
System.err.println(new Integer(1).equals(new Integer(1)));

你试过调试你的代码吗?使用调试器的能力非常重要。顺便说一句。在我看来,这个函数过于复杂了。任何适当的列表实现都应该有
get
length
方法,我只会使用这些方法访问我想要比较的元素。如果您在比较时打印值,您将获得mistakes@RahulAgrawal而不是打印值,他应该设置一个断点并逐步观察调试器中的值。正如Amongalen提到的:使用调试器是一项基本技能。