Day 61 of 100 Days LeetCode Challenge

LeetCode Challenge #234. Palindrome Linked List

Given the head of a singly linked list, return true if it is a 

 

 or false otherwise.

 

Example 1:

pal1linked list

Input: head = [1,2,2,1]
Output: true

Example 2:

pal2linked list

Input: head = [1,2]
Output: false

 

Constraints:

  • The number of nodes in the list is in the range [1, 105].
  • 0 <= Node.val <= 9
Video Solution
Java Solution
				
					class Solution {
    public boolean isPalindrome(ListNode head) {
        Stack<Integer> st = new Stack<>();

        ListNode ptr = head ;

        while(ptr!=null){
            st.push(ptr.val);
            ptr = ptr.next;
        }

        ListNode ptr2 = head ;

        while(ptr2!=null){
            int val1 = ptr2.val;
            int val2 = st.pop();

            if(val1 != val2 ){
                return false ;
            }
            ptr2 = ptr2.next;
        }

        return true ;
    }
}
				
			

Happy Coding with edSlash