首页 > Web开发 > 详细

234、回文链表 | JS

时间:2021-05-30 00:13:25      阅读:33      评论:0      收藏:0      [点我收藏+]

请判断一个链表是否为回文链表。

示例 1:

输入: 1->2
输出: false

示例 2:

输入: 1->2->2->1
输出: true
 1 /**
 2  * Definition for singly-linked list.
 3  * function ListNode(val, next) {
 4  *     this.val = (val===undefined ? 0 : val)
 5  *     this.next = (next===undefined ? null : next)
 6  * }
 7  */
 8 /**
 9  * @param {ListNode} head
10  * @return {boolean}
11  */
12 var isPalindrome = function(head) {
13     let temp = [];
14     while(head) {
15         temp.push(head.val);
16         head = head.next;
17     }
18     return temp.join(‘‘) === temp.reverse().join(‘‘)
19 };

234、回文链表 | JS

原文:https://www.cnblogs.com/oaoa/p/14826764.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!