Solution by raghav_prakash in Python


  • 0
    R
    class Solution:
        def isPalindrome(self, head):
            """
            :type head: ListNode
            :rtype: bool
            """
            ptr = head
            
            forward_str = self.getContents(ptr) 
            ptr = self.reverseList(head)
            reverse_str = self.getContents(ptr)
            
            if(forward_str == reverse_str):
                return True
            return False
    
        def getContents(self,ptr):
            str_ = ""
            
            while(ptr is not None):
                str_ += str(ptr.val)
                ptr = ptr.next
                
            return str_
        
        def reverseList(self,head):
            prev=None
            pres=head
            
            while(pres is not None):
                next_ = pres.next
                pres.next = prev
                
                prev=pres
                pres=next_
                
            return prev
    

Log in to reply
 

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