簡體   English   中英

檢查鏈表是否是回文(遞歸)

[英]Check if a linkedlist is palindrome (Recursively)

我正在嘗試下面的代碼,但結果始終為true

public boolean isPallindrome(Link link, Link right) {
    if (right == null)
        return true;

    if (!isPallindrome(link, right.getNext())) {
        return false;
    }
    boolean isP1 = right.getData() == link.getData();
    link = link.getNext();
    return isP1;
}

致電:-

System.out.println(link1.isPallindrome(link1.getFirst(), link1.getFirst()));

我認為罪魁禍首是針對null進行檢查的right的回報。 它可能總是返回true 有人可以建議如何解決此問題。

這就是您的算法的樣子

boolean flag=true;
public boolean checkPalindrome(List nodeList,boolean flag)
{
    if(flag==false)
        return false;
    if(nodeList.right==null)
        return flag;
    else{
        Node n;//reference for travelling till last node
        //traverse till last node

        //check first and last node

        if(same){
            //delete both nodes;
        }
        else{
            flag=false;
        }
        return checkPalindrome(nodeList,flag)

    }
}

這只是您需要繼續前進的指針。

如果根本需要再次返回原始列表,則可能要復制其他對象中的列表內容,然后在此方法中使用該對象

希望這可以幫助!

祝好運!

我可以幫你的忙。 您希望link.getNext()應該對其上一次調用起作用。 你能行的。 只需更改函數原型。

public boolean isPallindrome( Link &link ,鏈接權);

確保您的初始通話沒有受到影響。

老實說,我不遵循您的方法的一般邏輯。 我想提出一個完全不同的假設,並增加一些假設。

問題列表是否是雙向鏈接? 如果是這樣,那么您可以從兩端遍歷它,從外部在假設回文的任一端進行操作。 如果兩端不匹配,那么列表顯然不是回文。

您應該使用Java實用程序類(或至少實現接口),並且一定要為api添加書簽。 如果您有雙向鏈接列表(Java稱為雙端隊列),則查找pallindrome最快。 不幸的是,這種方法破壞了原始方法。

public static boolean <E> isPallindrome(Deque<E> deque) {
    if ( deque.size() <= 1 ) {
        return true;
    }
    E first = deque.removeFirst();
    E last = deque.removeLast();
    if ( deque.size() == 0 ) {
        return first.equals(last);
    }
    return isPallindrome(deque)
}

如果您想變得更高級,可以使用Iterators,然后跳過遞歸。

如果只有鏈接列表,那么您將需要一種到達列表末尾的方法。 一種方法是跟蹤列表的大小。 由於所有get ,最終結果為O(n^2)

public static boolean isPallindrome(List<?> list, int size) {
    if ( size <= 1 ) {
        return true;
    }
    if ( ! list.get(0).equals(list.get(size-1)) ) {
        return false;
    }
    return isPallindrome(list.subList(1,size-1));
}

問題出在:
鏈接= link.getNext(); 您的目的是使更改反映在所有遞歸的反向調用中,但是最終您要做的是僅在當前堆棧中本地更改引用,並聲明Left Link在后續調用中未反映。

如果是C,C ++,則可以將參數設置為Link ** left,Link * right,但是需要使用引用將Left Link靜態化為外部。

    public class PalindromeList {
    static Node left;
    public static void main(String[] args) {
        Node node = new Node(5);
        Node node1 = new Node(4, node);
        Node node2 = new Node(7, node1);
        Node node4 = new Node(6, node2);
        Node node5 = new Node(5, node4);
        left = node5;
        System.out.println(isPalindrome(node));
    }


    static boolean isPalindrome(Node right) {
        if (right == null)
            return true;
        boolean isp = isPalindrome(right.next);
        if (isp == false)
            return false;
        boolean isp1 = (right.value == left.value);
        left = left.next;
        return isp1;
    }
}

問題出在您的左指針移動。 您將需要在Java指針(始終是按值傳遞)中使用一些位於左指針上方的容器來激發雙指針(或稱為按引用傳遞)。 讓我們使用大小為1的數組作為容器,它將起作用。 我在這里使用不同的名稱。

public class LinkedListS<T> {

    protected Node head;
    public boolean isPalindrome()
    {
        Node storeHead = head;
        boolean result = isPalindromeUtil(new Node[]{head},head); //have to stimulate pass by reference
        head = storeHead;
        return result;
    }

    private boolean isPalindromeUtil(Node[] left,Node right)
    {
        if(right==null)
            return  true;

        boolean subResult = isPalindromeUtil(left,right.next);
        if (subResult==false)
            return false;

        boolean dataCompareResultForCurrentPosition = false;
        if(left[0].data == right.data)
            dataCompareResultForCurrentPosition = true;

        left[0]=left[0].next; //critical

        return dataCompareResultForCurrentPosition;
    }
}

實際上,繼續縮小清單。 在下面的代碼中,第一個調用將head放在第一個位置,而null放在最后一個位置。 在隨后的調用中,first將成為first.next,last將成為last,但列表中一個節點的address字段的值。

boolean palindromeCheck(List first, List last)
{
    //For even size of List
    if(first == last)
        return true;
    //For odd size of List.
    if(first.next == last)
        return true;
    List newLast = first.next;
    //Traverse till last element.
    while(newLast.next != last)
        newLast = newLast.next;
    //If value is not same, return false.
    if(first.val != newLast.val)
        return false;
    return palindromCheck( first.next, newLast );
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM