简体   繁体   English

Java,函数无法识别返回语句

[英]Java,function does not recognize the return statements

While writing a code for checking if a linked list is pallindrome or not,I created a reverseLL function which returns a reversed linked list and a isPallindrome function to check.The problem is that the return statement inside the loop is not being detected and only the last statement which is return true;在编写用于检查链表是否为回文的代码时,我创建了一个 reverseLL 函数,该函数返回一个反向链表和一个要检查的 isPallindrome 函数。问题是没有检测到循环内的 return 语句,只有最后一条语句返回真; is being executed every time: I am checking if the LL is pallindrome by dividing it into 2 parts and reversing the second half,then comparing both the halves每次都在执行:我通过将 LL 分成 2 部分并反转下半部分来检查 LL 是否是回文,然后比较两半

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;
}

I'm not going to run your code because it is not complete.我不会运行你的代码,因为它不完整。 However, it looks like you find the last element of the list and reverse it from there without taking into consideration that it also reverses the list yuo are already referring to with head .但是,看起来您找到了列表的最后一个元素并从那里反转它,而没有考虑到它也反转了 yuo 已经用head引用的列表。 So when you start the comparison loop you have a pointer to the first element in the reversed list and a pointer to the last element in the reversed list.所以当你开始比较循环时,你有一个指向反向列表中第一个元素的指针和一个指向反向列表中最后一个元素的指针。 You are definitely not splitting the list to two parts.您绝对不会将列表分成两部分。

Your code is also overly complicated.您的代码也过于复杂。 The correct algorithm is:正确的算法是:

find head and tail of list
while (head != tail) {
    if (head.value != tail.value)
        return false;
    head = head.next;
    tail = tail.prev;
}

You don't need two loop variables to find the tail of a linked list.您不需要两个循环变量来查找链表的尾部。 The correct algorithm is:正确的算法是:

tail = head
while (tail.next != null) {
    tail = tail.next;
}

Also, you can't generally compare Integers with equality operator.此外,您通常不能将整数与相等运算符进行比较。 You have to either unbox them to primitive ints or use equals.您必须将它们拆箱为原始整数或使用等于。 Try running:尝试运行:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM