每天刷一道牛客题霸-第23天- 判断一个链表是否为回文结构
题目
import java.util.*;
/*
* public class ListNode {
* int val;
* ListNode next = null;
* }
*/
public class Solution {
/**
*
* @param head ListNode类 the head
* @return bool布尔型
*/
public boolean isPail (ListNode head) {
// write code here
if(head == null){
return true;
}
StringBuilder stringList = new StringBuilder();
while(head != null){
stringList.append(String.valueOf(head.val));
head = head.next;
}
String r = stringList.toString();
String s = stringList.reverse().toString();
return s.equals(r);
}
}#牛客题霸##题解#
