背景
面试官:小A请坐,请实现一个函数来判断下回文链表; 小A:好的
题目
1 > 2 > 2 > 1
return true
1 > 2 > 2
return false
/**
* @param {ListNode} head
* @return {boolean}
* singly-linked list
* function ListNode(val, next) {
* this.val = (val === undefined ? 0 : val)
* this.next = (next === undefined ? null : next)
* }
*/
var isPalindrome = function(head) {
}
解题
var isPalindrome = function(head) {
let a = '', b = ''
while(head != null) {
a = a + head.val
b = head.val + b
head = head.next
}
return a === b
}
心得:
- 通过这种方便比较巧妙的解决,并且复杂度是o(n)
- 其实这种题目你说有啥技巧么,真的是想到了就想到了,随意最好是脑子记住了这种素材,在面试的时候才能发挥出来
- 现实问题中可能会有这种情况么,反正我是没发现
最后来波广告
web前端,vue/react均可,希望你有技术热情,欢迎发邮件到792300489@qq.com