每天刷一道牛客题霸-第23天- 判断一个链表是否为回文结构

题目

https://www.nowcoder.com/practice/3fed228444e740c8be66232ce8b87c2f?tpId=190&&tqId=35218&rp=1&ru=/ta/job-code-high-rd&qru=/ta/job-code-high-rd/question-ranking

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);
    }
}
#牛客题霸##题解#
全部评论

相关推荐

2 收藏 评论
分享
牛客网
牛客企业服务