Very simple Java O(n) Solution


  • -1
    W

    O(n) time but O(n) space, easy to understand.

    public class Palindrome_Linked_List {
        public boolean isPalindrome(ListNode head){
            LinkedList<Integer> list = new LinkedList<>();
            while (head!=null){
                list.add(head.val);
                head = head.next;
            }
            while(list.size()>1){
                if(list.pollFirst().equals(list.pollLast())){
                    return false;
                }
            }
            return true;
        }
    }

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.