Niuke Net Brushing Questions-Determine whether a linked list is a palindrome structure

Problem Description

Operate the given binary tree and transform it into a mirror image of the source binary tree.

Input description:
Input a linked list

Output description:
Whether the output linked list is a palindrome structure

Example

Example 1

Enter
[1,2,2,1]

Output
true

Solutions

analysis

  1. Solve through the queue, add all the nodes of the linked list to the double-ended queue, and pop the head element and the tail element in turn to make judgments
  2. By changing the structure of the right half (the code will be added later), changing the structure of the right half is equivalent to comparing the two sides to the middle

method

  1. Solved by queue

Code

public class Solution {
    
    
    public boolean isPail(ListNode head) {
    
    
        if (head == null) {
    
    
            return false;
        }
        // write code here
        LinkedList<Integer> list = new LinkedList<>();
        while (head != null) {
    
    
            list.add(head.val);
            head = head.next;
        }

        while (list.size() != 0) {
    
    
            if (list.pollFirst() != list.pollLast()) {
    
    
                return false;
            }
        }

        return true;
    }
}

If you want to test, you can go directly to the link of Niuke.com to do the test

Determine whether a linked list is a palindrome

Guess you like

Origin blog.csdn.net/qq_35398517/article/details/112913469