0
点赞
收藏
分享

微信扫一扫

LeetCode 234. 回文链表

乱世小白 2022-02-14 阅读 66

https://leetcode-cn.com/problems/palindrome-linked-list/

思路

  1. 链表元素值存入 list
  2. 双指针 (0 开始, 末尾开始) 判断值是否相等
 /**
        链表值加入集合
      双指针
     */
    public boolean isPalindrome(ListNode head) {
        List<Integer> list = new ArrayList<>();
        ListNode curr = head;
        while (curr != null) {
            list.add(curr.val);
            curr = curr.next;
        }
        int first = 0;
        int last = list.size() - 1;
        while (first < last) {

            if (list.get(first) != list.get(last)) {
                return false;
            }
            first++;
            last--;
        }
        return true;
    }
举报

相关推荐

0 条评论