当前位置:网站首页>LeetCode 234. Palindrome linked list

LeetCode 234. Palindrome linked list

2022-07-05 03:00:00 Abby's daily life

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

Ideas

  1. Linked list element value storage list
  2. Double pointer (0 Start , Start at the end ) Determine if the values are equal
 /**  Linked list values are added to the set   Double pointer  */
    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;
    }
原网站

版权声明
本文为[Abby's daily life]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202140829518510.html