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