Dont Know why my code is not working for odd length list


  • 0
    P
    enter code here
    

    {
    /**

    • Definition for singly-linked list.

    • struct ListNode {

    • int val;
      
    • ListNode *next;
      
    • ListNode(int x) : val(x), next(NULL) {}
      
    • };
      /
      class Solution {
      public:
      bool isPalindrome(ListNode
      head) {
      if(!head)
      return 1;
      if(!head->next)
      return 1;
      list<int> l;
      ListNode* temp1=head;
      ListNode* temp2=head;

       while(temp2 && temp2->next)
       {
           temp2=temp2->next->next;
           l.push_back(temp1->val);
           temp1=temp1->next;
       }
       while(temp1 )
       {
           int i=l.back();
           
           if(i!=temp1->val)
           {
               return 0;
               
           }
           l.pop_back();
           temp1=temp1->next;
       }
       return true;
      
    }
    

    };


Log in to reply
 

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