Palindrome string, palindrome linked list

There are some palindrome-related questions in leetcode, let's take a look at a few.

1. Determine whether the string is a palindrome

To determine whether a string is a palindrome, you only need to use double pointers to move from both ends of the string to the middle.

public class Huiwen {

    public static boolean huiwenStr(String s) {
        if (s == null || s.length() == 0) {
            return true;
        }

        int i=0, j=s.length()-1;
        while(i < j) {
            if (s.charAt(i) == s.charAt(j)) {
                i++;j--;
            } else {
                return false;
            }
        }
        return true;
    }

    public static void main(String[] args) {
        String s = "abeba";
        boolean result = huiwenStr(s);
        System.out.println(result);
    }
}

2. Determine whether a linked list is a palindrome linked list

The palindrome linked list means that the results of the positive order traversal and the reverse order traversal are the same. Because of the nature of a singly linked list, it cannot be traversed directly in reverse order, so the most direct way is to store a new linked list in reverse order and compare it with the original linked list.

public class ListNode {

    public int data;
    public ListNode next;

    public ListNode(int data) {
        this.data = data;
    }
    
}
public class HuiwenListV1 {

    public static ListNode init() {
        ListNode n1 = new ListNode(1);
        ListNode n2 = new ListNode(2);
        ListNode n3 = new ListNode(3);
        ListNode n4 = new ListNode(2);
        ListNode n5 = new ListNode(2);
        n1.next = n2;
        n2.next = n3;
        n3.next = n4;
        n4.next = n5;
        return n1;
    }

    public static ListNode reverse(ListNode root) {
        ListNode cur = root, pre = null;
        while (cur != null) {
            ListNode next = cur.next;
            cur.next = pre;
            pre = cur;
            cur = next;
        }
        return pre;
    }

    public static boolean isHuiWen(ListNode a) {
        if (a == null) return false;
        ListNode b = reverse(a);
        while (a != null) {
            if (a.data == b.data) {
                a = a.next;
                b = b.next;
            } else {
                return false;
            }
        }
        return true;
    }

    public static void main(String[] args) {
        ListNode root = init();
        boolean result = isHuiWen(root);
        System.out.println(result);
    }
}

If we do not use the above solution and use a recursive solution, the same effect can be achieved.

public class HuiwenListV2 {

    public static ListNode init() {
        ListNode n1 = new ListNode(1);
        ListNode n2 = new ListNode(2);
        ListNode n3 = new ListNode(3);
        ListNode n4 = new ListNode(2);
        ListNode n5 = new ListNode(1);
        n1.next = n2;
        n2.next = n3;
        n3.next = n4;
        n4.next = n5;
        return n1;
    }

    private static ListNode left = init();

    public static boolean ishuiwen(ListNode right) {
        if (right == null) return true;
        boolean result = ishuiwen(right.next);
        result = result && (right.data == left.data);
        left = left.next;
        return result;
    }

    public static void main(String[] args) {
        ListNode right = init();
        boolean result = ishuiwen(right);
        System.out.println(result);
    }
}

The above solution method draws on the common recursive routines of linked lists

public void traverse(ListNode head) {
    // 正序遍历逻辑
    traverse(head.next);
    // 逆序遍历代码
}

The core idea of ​​the above code is to first initialize right to root, then use the function to recursively move right to the end, and then compare right with left, which achieves a similar purpose of double pointer comparison.

Guess you like

Origin blog.csdn.net/bitcarmanlee/article/details/114210789