当前位置:   article > 正文

leetcode | go | 第234题 | 回文链表_leetcode 234 回文链表 golang

leetcode 234 回文链表 golang

回文链表

go

解决思路

  1. 当 head 非空,将 head 节点的值加入 list 数组中,head 遍历为下一个节点
  2. i、j 初始化为首尾索引值,每次往中间挪动一位,当对应的值不相等时,返回 false
  3. 遍历能结束,说明对应的值都相等,返回 true
  4. 空间不太好
  5. 题解思路:(1)将值赋值到数组中后用双指针法(2)递归(3)快慢指针

相关问题

  1. 注意不要弄错节点名称
  2. o(TヘTo)o(TヘTo)o(TヘTo)
  1. /**
  2. * Definition for singly-linked list.
  3. * type ListNode struct {
  4. * Val int
  5. * Next *ListNode
  6. * }
  7. */
  8. func isPalindrome(head *ListNode) bool {
  9. var list []int
  10. for head!=nil {
  11. list=append(list,head.Val)
  12. head=head.Next
  13. }
  14. for i,j:=0,len(list)-1;i<j;i,j=i+1,j-1 {
  15. if list[i]!=list[j] {
  16. return false
  17. }
  18. }
  19. return true
  20. }

本文内容由网友自发贡献,转载请注明出处:【wpsshop博客】
推荐阅读
相关标签
  

闽ICP备14008679号