public class ListNode {
int val;
ListNode next;
ListNode() {
}
ListNode(int val) {
this.val = val;
}
ListNode(int val, ListNode next) {
this.val = val; this.next = next;
}
}
//给定一个链表的 头节点 head ,请判断其是否为回文链表。
//如果一个链表是回文,那么链表节点序列从前往后看和从后往前看是相同的。
public class test01 {
public boolean isPalindrome(ListNode head) {
StringBuilder stringBuilder = new StringBuilder();
StringBuilder reverse = new StringBuilder();
while (head != null) {
stringBuilder.append(head.val);
reverse.append(head.val);
head = head.next;
}
reverse.reverse();
return stringBuilder.toString().equals(reverse.toString());
}
public static void main(String[] args) {
ListNode ln = new ListNode();
test01 ts = new test01();
System.out.println(ts.isPalindrome(ln));
}
}
备战蓝桥杯 Day_23
最新推荐文章于 2024-05-19 20:10:25 发布