Accepted Java Solution Using Stack


  • 1
    C
    /**
     * Definition for singly-linked list.
     * public class ListNode {
     *     int val;
     *     ListNode next;
     *     ListNode(int x) { val = x; }
     * }
     */
    public class Solution 
    {
        public boolean isPalindrome(ListNode head) 
        {
            Stack<Integer> s = new Stack<Integer>();
            ListNode temp = head;
            ListNode cur = head;
            while(temp != null)
            {
                s.push(temp.val);
                temp = temp.next;
            }
            while(cur != null)
            {
                if(cur.val != s.peek())
                    return false;
                else
                {
                    cur = cur.next;
                    s.pop();
                }
            }
            return true;
        }
    }

  • 0
    W

    In ur answer, u use O(n) space even u pop up all elements in the end


  • 3
    Y

    stack uses O(N) space, not O(1)


Log in to reply
 

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